UITableView循环通过单元格(我不希望它)

时间:2015-12-26 19:54:35

标签: ios swift loops tableview master-detail

我正在开发一个主 - 细节应用程序,其中我使用带有图像的自定义原型单元格和一些其他细节来编目。系统运行良好,直到你制作超过八个单元格。在你到达那一点之后,应用程序开始“循环”细胞。我不知道细胞是复制还是只是重新显示,但是如果你删除相邻细胞,迫使它们变成不同于8个细胞,有时另一个克隆会消失。看起来他们是纠缠在一起的东西。

自定义单元类:

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
}

}

MasterViewController:

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.
    }
}

}

感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

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

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

只需在此方法中清理单元格区域。