ios - 更改自定义单元格内容视图颜色
问题描述
我在主视图控制器中添加了一个更改主题按钮,其中有一个小表格视图。除了表格视图单元格的内容视图背景颜色外,我可以更改所有颜色,并且无法在 cellForRowAt 之外访问它。
我应该怎么办?有没有办法从我的自定义单元格中触发一个函数来改变颜色?
解决方案
您可以在控制器中添加一个属性来确定单元格的颜色。当你想改变颜色时,你打电话给tableView.reloadData()
. 这将cellForRowAt
在每个可见单元格上调用,您可以在此委托方法中更改颜色。
Your viewController
YourViewController: UIViewController {
fileprivate var cellColor = UIColor.blue
// where you change color
func changeColor() {
cellColor = UIColor.red
// this will make the delegate method `cellForRowAt` be called on each visible row
tableView.reloadData()
}
}
cellForRowAt:
// delegate method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuse id", for: indexPath) as! YourCustomCell
cell.contentView.backgroundColor = cellColor
// anything else...
return cell
}
推荐阅读
- javascript - MongoDB 嵌入式文档:大小限制和聚合性能问题
- ios - 有没有办法在firebase中运行节点脚本?
- swift - UIPressesEvent pressType Select 不能快速工作
- reactjs - 使用固定文本和占位符反应 Material-UI TextInput
- react-native - 如何为 react-native-camera 使用 refreshAuthorizationStatus 函数
- python - Python正则表达式替换单个换行符并忽略两个或多个换行符的序列
- c# - 如何在 Unity 中使用 System.Speech?
- node.js - 错误:NodeJS HTTPS 请求中的自签名证书
- laravel-admin - 无法在 z-song laravel-admin 中进行级联选择
- spring - 如何使用图像 url 更新图像