2015-02-23 188 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