在SWIFT中滚动时,我的表格视图将重用所选单元格

早上好

我的问题是,我的表格视图重复使用选定的单元格时,我再次向下滚动..我的意思是,当我从上向下select一个单元格,然后向下滚动,一些单元格,我没有select显示选定,也有一些选定的单元格没有显示select当我再次向上滚动,当发生的事情,我是苹果再次select不只是一个单元格,必须不被允许..我想提一提的是,我试图保存旧的索引path从“didSelectRowAtIndexPath”和我在“CellForRowAtIndexPath”中像这样检查它,但它不起作用:

if (old == indexpath) { cell?.backgroundColor = UIColor.redColor() cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color } else { cell?.backgroundColor = UIColor.whiteColor() } 

现在,这是我的代码

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell cell.tag = (indexPath.row) + 1 cell.textLabel?.text = Berufs[indexPath.row] var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40)) cell.selectionStyle = UITableViewCellSelectionStyle.None img.image = UIImage(named: "transparent.png") cell.indentationLevel = 1 cell.indentationWidth = 45 cell.addSubview(img) return cell } var old = 1000 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let indexPath = tableView.indexPathForSelectedRow() let cell = tableView.cellForRowAtIndexPath(indexPath!) var tmpCell = view.viewWithTag(old) var rowNum = (cell?.tag)! - 1 if (cell?.backgroundColor == UIColor.redColor()) { cell?.backgroundColor = UIColor.whiteColor() } else if (cell?.backgroundColor != UIColor.redColor()) { tmpCell?.backgroundColor = UIColor.whiteColor() cell?.backgroundColor = UIColor.redColor() cell?.textLabel?.backgroundColor = UIColor.whiteColor() println("du interssiert dich für \(Berufs[rowNum])") } old = rowNum + 1 } 

您当前将select状态存储在backgroundColor中。 这不是一个好主意,因为出于性能原因,单元格将被tableView重用。

您应该将选定的indexPaths保存在数据模型中。 如果你想允许多个select,你可以将select的indexPaths存储在一个NSMutableSet中。

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell cell.selectionStyle = .None configure(cell, forRowAtIndexPath: indexPath) return cell } var selectedIndexPaths = NSMutableSet() func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { cell.textLabel!.text = "Row \(indexPath.row)" if selectedIndexPaths.containsObject(indexPath) { // selected cell.backgroundColor = UIColor.redColor() } else { // not selected cell.backgroundColor = UIColor.whiteColor() } } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if selectedIndexPaths.containsObject(indexPath) { // deselect selectedIndexPaths.removeObject(indexPath) } else { // select selectedIndexPaths.addObject(indexPath) } let cell = tableView.cellForRowAtIndexPath(indexPath)! configure(cell, forRowAtIndexPath: indexPath) } 

如果你只需要单一的select,你应该保存选定的indexPath在一个NSIndexPath? 实例variables

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell cell.selectionStyle = .None configure(cell, forRowAtIndexPath: indexPath) return cell } var selectedIndexPath: NSIndexPath? func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { cell.textLabel!.text = "Row \(indexPath.row)" if selectedIndexPath == indexPath { // selected cell.backgroundColor = UIColor.redColor() } else { // not selected cell.backgroundColor = UIColor.whiteColor() } } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if selectedIndexPath == indexPath { // selected same cell -> deselect all selectedIndexPath = nil } else { // select different cell let oldSelectedIndexPath = selectedIndexPath selectedIndexPath = indexPath // refresh old cell to clear old selection indicators var previousSelectedCell: UITableViewCell? if let previousSelectedIndexPath = oldSelectedIndexPath { if let previousSelectedCell = tableView.cellForRowAtIndexPath(previousSelectedIndexPath) { configure(previousSelectedCell, forRowAtIndexPath: previousSelectedIndexPath) } } } let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! configure(selectedCell, forRowAtIndexPath: indexPath) } 

我刚刚有同样的问题,我做了什么,我添加了一个名为isset的新variables(Bool)给CustomTableViewCell,当我第一次创build单元格时,我将其更改为true,所以在我想重用一个已经设置的单元格,我刚刚返回了一个设置,不设置是:

 let cell = tableView.dequeueReusableCellWithIdentifier("Cellidentifier", forIndexPath: indexPath) as CustomTableViewCell if cell.isset { return cell } //set your cell here cell.isset = true return cell