ios - UITableView willDisplayCell 方法的不当行为

标签 ios swift uitableview

有一个 UITableView 的帖子。
已查看的帖子 ID 保存在 sqlite 中
我想显示,看到的帖子是橙色的,其他的是黑色的。
但是当我在 willDisplayCell 方法中为 seen post 设置橙色时,一些单元格被错误地着色为橙色,否则打印日志(“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”日志。

最佳答案

尝试完成 if 语句

 if (post.isRead.boolValue == true) {
    print("Color it")
    cellPost!.body.textColor = UIColor.orangeColor()
    cellPost!.title.textColor = UIColor.orangeColor()
}else{
    cellPost!.body.textColor = UIColor.blackColor()
    cellPost!.title.textColor = UIColor.blackColor()}

关于ios - UITableView willDisplayCell 方法的不当行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44519945/

相关文章:

ios - 使用 NSArray 和 NSDictionary : Can not convert value of type "string" to expected argument type "int"

objective-c - 在 iOS 程序中比较 char 数组和字符串

ios - Sprite Kit 碰撞次数超过 1 次

ios - UITableView 在 iPad (ios7) 上显示意外的行 :

JavaFX:在TableView的同一单元格中添加文本和进度条

ios - Firebase 扇出数据以更新特定字段删除其他兄弟字段

ios - 如何读取动画后的keyPath值?

ios - 向 Firebase 中的其他用户发送预先编写的应用内用户通知

ios - 从字符串转换为具有当前时区的日期

ios - 以编程方式将 View 添加到 TableView 中的单元格