Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView willDisplayCell方法的错误行为_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView willDisplayCell方法的错误行为

Ios UITableView willDisplayCell方法的错误行为,ios,swift,uitableview,Ios,Swift,Uitableview,有一个帖子的UITableView。 看到的帖子id保存在sqlite中 我想展示,看到的帖子是橙色的,其他帖子是黑色的。 但是,当我在willDisplayCell方法中为SEED post设置橙色时,有些单元格的颜色不正确,否则打印日志(“color it”)是正确的 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPat

有一个帖子的
UITableView

看到的帖子id保存在sqlite中
我想展示,看到的帖子是橙色的,其他帖子是黑色的。
但是,当我在
willDisplayCell
方法中为SEED 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”日志。

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

1.理解可重用的表视图单元格对象

出于性能原因,表视图的数据源通常应该重用 UITableViewCell 对象将单元格指定给其中的行时 表格视图(uu:cellForRowAt:) 方法。表视图维护一个队列或列表 UITableViewCell 数据源已标记为可重用的对象。当要求为表视图提供新单元格时,从数据源对象调用此方法

如果现有单元格可用或创建新单元格,则此方法将其出列 使用以前注册的类或nib文件的新文件

如果没有可重用的单元格,并且您没有注册类或nib文件,则此方法返回nil

2.prepareforeuse()的用法

如果UITableViewCell对象是可重用的,也就是说,它有一个重用标识符。在从UITableView方法返回对象之前,将调用此方法 dequeueReusableCell(带标识符:) . 出于性能原因

您应该只重置与该单元格无关的单元格属性 内容,例如alpha、编辑和选择状态

中的表视图的委托 表格视图(uu:cellForRowAt:) 重复使用单元格时,应始终重置所有内容。如果单元对象没有关联的重用标识符,则不会调用此方法。如果重写此方法,则必须确保调用超类实现


手动重置@RJiryes已经描述过的单元格属性的另一种方法。

谢谢。我不想这样做。我知道它解决了这个问题,但是为什么我需要在默认颜色为黑色时再次使用黑色?关于为什么不使用CellForRowAtIndexPath设置单元格样式的具体原因,请使用自定义cells@SandeepBhandari我在DataSource中使用了CellForRowatineXpath,我不想在DataSource中设置样式,然后在数据库中创建名为configCell的函数cell子类并调用cellForRowAtIndexPath中的configCell。这更干净,因为单元格负责设置自己的样式:)