简体   繁体   English

UITableView willDisplayCell 方法的错误行为

[英]Misbehavior of UITableView willDisplayCell method

There is a UITableView of posts.有一个UITableView的帖子。
Seen posts id's are saved in sqlite看到的帖子 ID 保存在 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.但是当我在willDisplayCell方法中为看到的帖子设置橙色时,一些单元格的颜色不正确,否则打印日志(“着色”)是正确的。

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()
    }
}

For instance, if just one post is seen, "Color it" is printed once.例如,如果只看到一个帖子,“Color it”就会打印一次。 and it's correct.这是正确的。 But some other cells are colored orange without "Color it" log.但是其他一些单元格是橙色的,没有“着色”日志。

Try completing the if statement尝试完成 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.Understanding of Reusable table-view cell object 1.Reusable table-view cell对象的理解

From Apple Documentation来自苹果文档

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method.出于性能原因,表视图的数据源通常应在其tableView(_:cellForRowAt:)方法中将单元格分配给行时重用UITableViewCell对象。 A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.表视图维护数据源已标记为重用的 UITableViewCell 对象的队列或列表。 Call this method from your data source object when asked to provide a new cell for the table view.当要求为表格视图提供新单元格时,从数据源对象调用此方法。

This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered.如果现有单元格可用,则此方法使现有单元格出列,或者使用您之前注册的类或 nib 文件创建一个新单元格。

If no cell is available for reuse and you did not register a class or nib file, this method returns nil.如果没有可重用的单元格并且您没有注册类或 nib 文件,则此方法返回 nil。

2.Usage of prepareForReuse() 2.prepareForReuse()的使用

From Apple Documentation来自苹果文档

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCell(withIdentifier:) .如果 UITableViewCell 对象是可重用的——也就是说,它有一个重用标识符——这个方法会在对象从 UITableView 方法dequeueReusableCell(withIdentifier:)返回之前调用。 For performance reasons,出于性能原因,

you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state.您应该只重置与内容无关的单元格属性,例如 alpha、编辑和选择状态。

The table view's delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell. tableView(_:cellForRowAt:) 中表格视图的委托在重用单元格时应始终重置所有内容。 If the cell object does not have an associated reuse identifier, this method is not called.如果单元格对象没有关联的重用标识符,则不会调用此方法。 If you override this method, you must be sure to invoke the superclass implementation.如果覆盖此方法,则必须确保调用超类实现。

Another way to do manually resetting attributes of the cell which already described by @RJiryes.另一种手动重置@RJiryes 已经描述的单元格属性的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM