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

时间:2016-05-15 14:34:33

标签: ios swift 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
}

当我一直向下滚动时,它会起作用 - 最后一个单元格会突出显示。但是,当我向上和向下滚动时,表格中间的其他单元格也会突出显示。

有没有办法阻止它?

谢谢!

1 个答案:

答案 0 :(得分:3)

您必须撤消if - 分支对所有其他单元格所做的更改:

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

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

相关问题