2015-02-23 108 views
0

我有一个奇怪的问题,我的tableView。 我有一个音轨列表和一个音频播放器的segue,以播放特定行中的选定音轨。一切正常!Swift TableViewCell问题:改变选定行的背景颜色

我想改变表格中所选行的背景颜色,这样,一旦用户播放音频并返回到轨道列表(我的表格视图控制器),他可以看到哪些是先前选择的行。

但是,当我运行它不仅改变了我选择的索引路径上的行的颜色,而且还改变了索引路径+ 10上的项目。 如果我选择第一行,它会将我的行颜色更改为指数:0,10,20,30 ...

为了改变我做后续选定单元格的颜色:

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

请找我的问题的截图,我已经选择只有三排:1,3,5,但我得到选择1,3,5,11,13,15,21,23等......: https://www.dropbox.com/s/bhymu6q05l7tex7/problemaCelleColore.PNG?dl=0

进一步的细节 - 如果能帮助 - 这里是我的自定义表视图类:

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 

这是我TableViewController方法的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!) 


    return cell 

} 

我在iPad上航运行与iOS 7.1。

非常感谢您提出任何有关我的问题的建议或建议。

回答

0

这可能是因为UITableViewCells被回收。这意味着先前选择的tableViewCell被较低索引处的单元重新使用。这是UITableView的预期行为,并且有意义,因为它可以节省内存使用量。为了解决这个问题,你需要让你的数据源保持跟踪选择哪个单元格,并相应地更新单元格的背景颜色。

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 
} 

然后在您的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 
} 
+0

谢谢道尔顿您回应并对不起我的愚蠢的问题,而是:在我的情况,我的数据来源将是我的CustomTableViewCell呢? – Fustav 2015-02-26 16:52:28