My table view reuses selected cells when scrolling through SWIFT

Good morning

My problem is that my table scan reuses the selected cell when I scroll down and up again. I mean, when I select one cell from the top and then scroll down, some cells that I did not select are shown selected, and also some selected cells from up are not displayed, when I scroll up again, and when this happens, I add the apple again to select more than one cell that should be banned. I want to mention that I tried to save the old index path from 'didSelectRowAtIndexPath' and I checked it like this: "CellForRowAtIndexPath", but it does not work:

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() } 

Now here is my code

  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 } 
+7
ios uitableview swift
source share
2 answers

You are currently saving the selection state in backgroundColor. This is not a good idea, because for performance reasons the cells will reuse the tableView.

You must save the selected indexPaths in the data model. If you want to enable multiple selections, you can save the selected indexes to an 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) } 

If you need only one choice, you have to save the selected indexPath to NSIndexPath? instance variable

 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) } 
+14
source share

I had the same problem some time ago that I did, I added a new variable (Bool) called isset to CustomTableViewCell, and when I created the cell for the first time, I changed it to true, so after I wanted to reuse the already set cell, I just returned the installed arleady set so as not to install again:

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

All Articles