UITableView willDisplayCell方法的不当行为 [英] Misbehavior of UITableView willDisplayCell method

查看:165
本文介绍了UITableView willDisplayCell方法的不当行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个帖子UITableView.
看到的帖子ID保存在sqlite中
我想显示橙色的帖子,其他黑色的帖子.
但是,当我在willDisplayCell方法中为看到的帖子设置橙色时,某些单元格的颜色会错误地显示为橙色,否则打印日志(为其着色")是正确的.

There is a UITableView of posts.
Seen posts id's are saved in sqlite
I want to show, seen posts in orange color and others in black.
But when I set orange color for seen post in willDisplayCell method some cells are colored orange incorrectly, otherwise the print log ("Color it") is correct.

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"将被打印一次.这是正确的.但是其他一些单元格则显示为橙色,而没有将其着色"日志.

For instance, if just one post is seen, "Color it" is printed once. and it's correct. But some other cells are colored orange without "Color it" log.

推荐答案

尝试完成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()}

这篇关于UITableView willDisplayCell方法的不当行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆