我正在尝试使用以下代码突出显示我的表格 View 中的第一个单元格:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
cell.layer.borderWidth = 0
cell.layer.borderColor = nil
cell.layer.borderWidth = 2
cell.layer.borderColor = UIColor(red:0.38, green:0.69, blue:0.16, alpha:1.0).cgColor
}
}
一切似乎都很好。但是当我点击某个单元格,转到另一个 viewController,然后返回我的单元格时,不知何故第二个单元格已经突出显示。所以我点击了我的单元格几次,发现在我从另一个 View Controller 返回到 tableview 后,下一个单元格已经突出显示(并且在 N 单击后我的所有单元格都用边框突出显示)。
我应该如何修复我的代码以仅突出显示第一个单元格,即使我转到另一个 Controller 并返回到我的单元格?
最佳答案
细胞得到重用。当您为给定条件设置任何属性时,您必须始终为所有其他条件重置该属性。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
cell.layer.borderWidth = 2
cell.layer.borderColor = UIColor(red:0.38, green:0.69, blue:0.16, alpha:1.0).cgColor
} else {
cell.layer.borderWidth = 0
cell.layer.borderColor = nil
}
}
关于ios - 在 uitableview, IOS 中突出显示第一个单元格时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349354/