Ios UITableView:高亮显示最后一个单元格,但其他单元格也会高亮显示
我有一个UITableView,作为SWReveliveViewController的一部分用作滑动菜单 我想在UITableView中选择最后一个单元格并实现以下操作:Ios UITableView:高亮显示最后一个单元格,但其他单元格也会高亮显示,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,作为SWReveliveViewController的一部分用作滑动菜单 我想在UITableView中选择最后一个单元格并实现以下操作: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let customCell = tableView.dequeueReusable
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
}
产生不良副作用的原因是细胞的重复使用。创建一个单元格,然后将其用作最后一个单元格,然后将其移出屏幕并在其他地方重新使用。它仍然包含更改的颜色信息,但不再位于相应的位置