swift - 具有动态 backgroundColor 的 Xcode 静态单元格

标签 swift xcode uitableview

是否可以在 UITableView 中使用静态单元格设置动态背景颜色?

例如,用户按下 UITableView 中的一个按钮,然后同一 View 中的所有单元格都变为特定颜色。

我已经想出了如何在 View 显示时设置背景颜色:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.black
}

现在我想在 View 已经加载时实现相同的效果。有什么建议吗?

最佳答案

您可以通过 visibleCells 访问当前可见的单元格(那些已经过了 willDisplay 里程碑的单元格)属性(property)。您可以遍历此数组并执行所需的任何操作:

for cell in tableView.visibleCells {
    cell.backgroundColor = UIColor.black
}

或者,您可以在 tableView 上调用 reloadData(),这将导致重新创建和重新显示所有单元格。

关于swift - 具有动态 backgroundColor 的 Xcode 静态单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504169/

相关文章:

iphone - 在 UITableView 中以编程方式移动带动画的行

ios - swift:将字符串转换为 double 时出现问题

xcode - 如何在命令行上使用 Intel fortran 编译器和 MKL

ios - 3rd 方框架 swift 文件中的编译器错误

ios - Xcode 中的约束变灰

iphone - 触摸时更改详细信息披露指示符图像

iphone - 从 Nib 加载时,自定义单元永远不会排队?

swift - 动态修复屏幕中的 UICollectionView 单元格

ios - 企业(内部)Swift 应用程序在 iOS9 上启动时退出

iOS map 套件 : tap in callout touches annotationView that is rendered under the callout