且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 12:05:58

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

You have to undo the change made in the if-branch for all other cells:

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

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

The reason for the undesired side effect is the reusing of cells. A cell gets created, then it gets used as the last cell, then it moves off-screen and is reused somewhere else. It still contains the changed color information but is no longer at the corresponding position.