UITableView:突出显示最后一个单元格,但其他单元格也突出显示
问题描述:
我有UITableView作为SWRevealViewController的一部分用作滑动菜单。UITableView:突出显示最后一个单元格,但其他单元格也突出显示
我想选择在UITableView中的最后一个单元格并执行以下操作:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! IGAWindAloftMenuTableViewCell
...
let sectionsAmount = tableView.numberOfSections
let rowsAmount = tableView.numberOfRowsInSection(indexPath.section)
if (indexPath.section == sectionsAmount - 1 && indexPath.row == rowsAmount - 1)
{
customCell.backgroundColor = UIColor.yellowColor()
}
return customCell
}
当我滚动一路下跌,它的工作原理 - 最后一个单元格被突出显示。但是,当我上下滚动时,表格中间的其他单元格也会突出显示。
有什么办法可以预防它吗?
谢谢!
答
必须撤消在if
分枝为所有其他细胞进行了更改:
if (indexPath.section == sectionsAmount - 1 && indexPath.row == rowsAmount - 1) {
customCell.backgroundColor = UIColor.yellowColor()
} else {
customCell.backgroundColor = UIColor.whiteColor() // or whatever color
}
的原因不想要的副作用是细胞的重用。一个单元格被创建,然后它被用作最后一个单元格,然后它离开屏幕并在其他地方重用。它仍包含已更改的颜色信息,但不再位于相应的位置。
非常感谢!这很好。干杯。 –