ma vue de table réutilise les cellules sélectionnées lors du défilement dans SWIFT

Bonjour

mon problème est que ma vue de table réutilise la cellule sélectionnée quand je fais défiler vers le bas et vers le haut .. je veux dire quand je choisis une cellule d'en haut puis défile vers le bas, certaines cellules que je n'ai pas sélectionnées sont montrées pas montré sélectionné quand je fais défiler vers le haut à nouveau, et quand cela est arrivé je suis à nouveau pomme pour sélectionner plus d'une seule cellule qui doit être interdit .. je tiens à mentionner que j'ai essayé de sauvegarder l'ancien path d'index de 'didSelectRowAtIndexPath' vérifié comme ça dans le 'CellForRowAtIndexPath'mais ça ne marche pas:

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

Maintenant, voici mon 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 } 

Vous stockez actuellement l'état de sélection dans backgroundColor. Ce n'est pas une bonne idée car pour des raisons de performances, les cellules seront réutilisées par la tableView.

Vous devez save les indexPaths sélectionnés dans le model de données. Si vous souhaitez autoriser la sélection multiple, vous pouvez stocker les indexPaths sélectionnés dans un 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) } 

Si vous n'avez besoin que d'une seule sélection, vous devez save l'indexPath sélectionné dans un NSIndexPath? variable d'instance

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

J'ai eu le même problème il y a un moment, ce que j'ai fait, j'ai ajouté une nouvelle variable (Bool) nommée isset à CustomTableViewCell, et quand j'ai créé la cellule pour la première fois, je l'ai changé en vrai, donc après je voulais réutiliser une cellule déjà définie, je viens de renvoyer l'arleady set one, de ne pas définir est à nouveau:

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