/ / Problème avec Swift TableViewCell: changer la couleur d'arrière-plan pour la ligne sélectionnée - swift, tableviewcell, uibackgroundcolor

Problème avec Swift TableViewCell: change la couleur d’arrière-plan de la ligne sélectionnée - swift, tableviewcell, uibackgroundcolor

J'ai un problème étrange avec ma tableView. J'ai une liste de pistes audio et une transition vers un lecteur audio afin de lire la piste sélectionnée sur une ligne spécifique. Tout fonctionne bien!

Je voulais changer la couleur d'arrière-plan duligne sélectionnée dans le tableau afin que, une fois que l'utilisateur a lu l'audio et qu'il soit revenu à la liste des pistes (mon Table View Controller), il peut voir quelles sont les lignes précédemment sélectionnées.

Mais quand je cours ça me change la couleur non seulementpour la ligne au chemin d'index que j'ai sélectionné, mais aussi à l'élément au chemin d'index + 10. Si je sélectionne la première ligne, cela me change la couleur de la ligne à l'index: 0, 10, 20, 30 ...

Afin de changer la couleur de la cellule sélectionnée, j'ai fait ce qui suit:

// MARK: - Navigation
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)

var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath) as CustomTableViewCell
selectedCell.contentView.backgroundColor = UIColor.greenColor()
selectedCell.backgroundColor = UIColor.blueColor()

Veuillez trouver une capture d'écran de mon problème, je n'ai sélectionné que trois lignes: 1, 3, 5 mais je suis sélectionné 1,3,5,11,13,15,21,23 et ainsi de suite ...: https://www.dropbox.com/s/bhymu6q05l7tex7/problemaCelleColore.PNG?dl=0

Pour plus de détails - si cela peut aider - voici ma classe de vue de table personnalisée:

    import UIKit

class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var artista: UILabel!

@IBOutlet weak var brano: UILabel!

var ascoltato = false

@IBOutlet weak var labelRiproduciAscoltato: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

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

// Configure the view for the selected state
}

func setCell(artista: String, brano: String){
self.artista.text = artista
self.brano.text = brano
}

}  // END MY CUSTOM TABLE VIEW CELL

Voici la méthode cellForRowAtIndexPath dans mon TableViewController:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell

var tracks : Brani  //Brani is my custom Object for handle my tracks

cell.setCell(tracks.title!, brano: tracks.author!)


return cell

}

Je cours sur iPad Air avec iOS 7.1.

Merci d'avance pour toute suggestion ou conseil lié à mon problème.

Réponses:

0 pour la réponse № 1

C'est probablement parce que UITableViewCells sontrecyclé. Cela signifie que la tableViewCell précédemment sélectionnée est réutilisée par les cellules des index inférieurs. Il s'agit du comportement attendu d'un UITableView et il est logique, car il économise l'utilisation de la mémoire. Pour résoudre le problème, vous devrez demander à votre source de données de garder une trace de la cellule sélectionnée et de mettre à jour la couleur d'arrière-plan de la cellule en conséquence.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)
//datasource is updated with selected state
//cell is updated with color change
}

Ensuite, dans votre méthode cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell

var tracks : Brani  //Brani is my custom Object for handle my tracks

cell.setCell(tracks.title!, brano: tracks.author!)
//update cell style here as well (by checking the datasource for selected or not).

return cell
}