【发布时间】:2017-06-13 11:14:01
【问题描述】:
有一个UITableView 的帖子。
看到的帖子 ID 保存在 sqlite
我想显示,看到橙色的帖子和黑色的其他帖子。
但是,当我在willDisplayCell 方法中为看到的帖子设置橙色时,一些单元格的橙色颜色不正确,否则打印日志(“Color it”)是正确的。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let post = postDataSource.posts[indexPath.row]
print(post.id)
let cellPost = cell as? PostListViewCell
if post.isRead.boolValue == true {
print("Color it")
cellPost!.body.textColor = UIColor.orangeColor()
cellPost!.title.textColor = UIColor.orangeColor()
}
}
例如,如果只看到一篇帖子,“Color it”会打印一次。这是正确的。但是其他一些单元格在没有“Color it”日志的情况下被染成橙色。
【问题讨论】:
-
关于为什么不使用 CellForRowAtIndexPath 设置单元格样式的任何具体原因?
-
使用自定义单元格
-
@SandeepBhandari 我在 DataSource 中使用了 CellForRowAtIndexPath,我不想在 DataSource 中设置样式
-
然后在您的单元子类中创建名为 configCell 的函数,并在 cellForRowAtIndexPath 中调用 configCell。这更干净,因为单元负责设置自己的样式:)
标签: ios swift uitableview