早上好
我的问题是,当我向下滚动并再次向上滚动时,我的表视图重用了已选择的单元格。也就是说,当我从上面选择一个单元格然后向下滚动时,一些我没有选择的单元格会显示为已选择状态,而当我再次向上滚动时,一些从上面选择的单元格不再显示为已选择状态。当这种情况发生时,我又能够选择多个单元格,但这是不允许的。我想提一下,我尝试过在'didSelectRowAtIndexPath'中保存旧的indexPath,并在'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
}