2015-02-06 57 views
7

早安我的表視圖重用選定單元格時滾動 - 在SWIFT

我的問題是,我的表視圖重用選定單元格時,我向下滾動,再升..我的意思是,當我選擇從一個細胞然後向下滾動,一些我沒有選擇的單元格顯示被選中,當我再次向上滾動時,從上向下選擇的一些單元格也不會被選中,當發生這種情況時,我再次選擇蘋果以選擇不僅僅是一個單元格,允許..我想提到的是,我試圖保存從'didSelectRowAtIndexPath'舊索引路徑,我在'CellForRowAtIndexPath'像這樣檢查它,但它不工作:

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

現在,這裏是我的代碼

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 

} 

回答

14

您目前選擇狀態存儲在的backgroundColor 。這不是一個好主意,因爲出於性能原因,單元格將被tableView重用。

您應該將選定的indexPaths保存在數據模型中。如果你想允許多個選擇,你可以將選擇的indexPaths存儲在一個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) 
} 

如果您只需要單一選擇,您應該將選定的indexPath保存在NSIndexPath中?實例變量

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

作品像一個魅力* __ * thankssssssss – 2015-02-06 11:17:54

+0

這個答案是偉大的,作品像一個魅力,但爲什麼你有覆蓋一些功能?作爲只要控制器實現UITableViewDelegate,你應該只有這個功能,但沒有「覆蓋」吧? – 2016-05-06 01:17:09

+0

謝謝你也節省了我的時間:) – user1553381 2017-05-23 16:45:59

-1

我前一陣子有同樣的問題,我所做的是,我添加了一個名爲isset到CustomTableViewCell新變量(布爾),當我創建了細胞的第一次,我把它改成真實的,所以以後我想重新使用已設置的細胞,我剛回國的arleady設置一個,不設置又是:

let cell = tableView.dequeueReusableCellWithIdentifier("Cellidentifier", forIndexPath: indexPath) as CustomTableViewCell 

if cell.isset 
{ 
     return cell 
} 

//set your cell here 
cell.isset = true 
return cell 
+0

我沒有找到這個函數'isset',你是自己做的還是內置的? – 2015-02-06 08:35:12

+0

你有沒有試過運行代碼,或者你有錯誤?有時XCode不顯示所有的方法。你確實在cellForRowAtIndexPath中實現了這個代碼嗎? – 2015-02-06 08:37:00

+0

它顯示了一個錯誤..是的..我做到了 – 2015-02-06 08:38:44

相關問題