UITableView:突出显示最后一个单元格,但其他单元格也会突出显示

我有UITableView作为SWRevealViewController的一部分用作滑动菜单。

我想在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 -branch中针对所有其他单元格所做的更改:

 if (indexPath.section == sectionsAmount - 1 && indexPath.row == rowsAmount - 1) { customCell.backgroundColor = UIColor.yellowColor() } else { customCell.backgroundColor = UIColor.whiteColor() // or whatever color } 

不希望的副作用的原因是细胞的重复使用。 创建一个单元格,然后将其用作最后一个单元格,然后将其移出屏幕并在其他位置重复使用。 它仍然包含已更改的颜色信息,但不再位于相应的位置。