我的問題是,我的表視圖重用選定單元格時,我向下滾動,再升..我的意思是,當我選擇從一個細胞然後向下滾動,一些我沒有選擇的單元格顯示被選中,當我再次向上滾動時,從上向下選擇的一些單元格也不會被選中,當發生這種情況時,我再次選擇蘋果以選擇不僅僅是一個單元格,允許..我想提到的是,我試圖保存從'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
}
作品像一個魅力* __ * thankssssssss – 2015-02-06 11:17:54
這個答案是偉大的,作品像一個魅力,但爲什麼你有覆蓋一些功能?作爲只要控制器實現UITableViewDelegate,你應該只有這個功能,但沒有「覆蓋」吧? – 2016-05-06 01:17:09
謝謝你也節省了我的時間:) – user1553381 2017-05-23 16:45:59