ios - 在 uitableview, IOS 中突出显示第一个单元格时遇到问题

标签 ios swift uitableview

我正在尝试使用以下代码突出显示我的表格 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/

相关文章:

ios - 设置 UIScrollView 的 contentInset 为 KeyboardWillShowNotification 无法正常工作

ios - 在对 tableView 约束进行动画处理时,TableViewCells 最初的大小错误

ios - 圆角半径不适合动态分组的 tableView swift

ios - AF网络 block 和提要

ios - CATextLayer 上来自 iOS 6 的不需要的垂直填充

iOS:多个 AU 与一个渲染回调中的所有内容

ios - 如何使用 iOS SpriteKit 在环形内强制触摸?

ios - 如何在短时间内将新插入的 UITableViewCell 的 alpha 设置为 0

ios - 带有 UIImage 的 GMSPolygon 或 GMSGroundOverlay

ios - 在 iOS 8 中解压一个 .tar 文件