UITableView 循环遍历单元格(我不希望它这样做)

UITableView is Looping Through Cells (That I Don't Want it to)

我正在开发一个主从应用程序,我在其中使用带有图像和一些其他细节的自定义原型单元格来对事物进行分类。该系统运行良好,直到您制造出八个以上的电池。达到这一点后,应用程序开始 'loop through' 单元格。我不知道这些单元格是被复制还是只是重新显示,但如果你删除相邻的单元格迫使它们变得不同于八个单元格,有时另一个克隆会消失。好像纠缠什么的。

自定义单元格Class:

import UIKit

class UITableViewToolCell: UITableViewCell {
@IBOutlet weak var thumbnailImage: UIImageView!
@IBOutlet weak var categoryImage: UIImageView!
@IBOutlet weak var sellerImage: UIImageView!

@IBOutlet weak var itemTitle: UILabel!
@IBOutlet weak var itemLocation: UILabel!

var name = "New Item"
var location = "Unknown"
var soldBy = "nil"
var category = "nil"
var thumbnail = "nil"
var information = "Information Not Available"

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    name += " \(arc4random_uniform(5000))"
    itemTitle.text = name
    itemLocation.text = location
    sellerImage.image = UIImage(named: soldBy)
    categoryImage.image = UIImage(named: category)

    let image : UIImageView = thumbnailImage
    image.layer.borderWidth=2.0
    image.layer.masksToBounds = false
    image.layer.borderColor = UIColor.whiteColor().CGColor
    image.layer.cornerRadius = 13
    image.layer.cornerRadius = image.frame.size.height/2
    image.clipsToBounds = true
    image.image = UIImage(named: thumbnail)
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

主视图控制器:

import UIKit

class MasterViewController: UITableViewController , UISearchBarDelegate, UISearchDisplayDelegate {

var detailViewController: DetailViewController? = nil
var objects = [UITableViewToolCell]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem()
    tableView.rowHeight = 111
    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
    UINavigationBar.appearance().barTintColor = UIColor.purpleColor()
    tableView.backgroundColor = UIColor(red:0.18, green: 0.18, blue: 0.18, alpha: 1.0)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightRegular)], forState: UIControlState.Normal)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightRegular)]
    addButton.tintColor = UIColor.whiteColor();
    self.navigationItem.rightBarButtonItem = addButton
    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }
}

override func viewWillAppear(animated: Bool) {
    self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
    super.viewWillAppear(animated)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func insertNewObject(sender: AnyObject) {
    let t = UITableViewToolCell()
    objects.insert(t, atIndex: 0)
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController

            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ToolCell", forIndexPath: indexPath) as! UITableViewToolCell
    objects[indexPath.row] = cell
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
    cell.selectedBackgroundView = backgroundView
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        objects.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

}

感谢任何帮助。

我认为您需要在自定义单元格中实现:

func prepareForReuse() {
    super.prepareForReuse()
    itemTitle.text = ""
    //... and so on
}

只需在此方法中清理单元格的字段。