美文网首页
iOS tableView 复用自定义的 cell 导致的数据错

iOS tableView 复用自定义的 cell 导致的数据错

作者: 赫子丰 | 来源:发表于2019-03-07 21:03 被阅读0次

两种解决方案:

1、弃用 cell 的复用机制
2、复用时清空 cell 的数据

用第一种的话在数据较多时会降低效率,而且没有从根本上解决问题。

第二种解决可以通过在自定义的 cell 中重写 prepareForReuse 方法实现:

- (void)prepareForReuse NS_REQUIRES_SUPER;                                                      
// if the cell is reusable (has a reuse identifier), 
//this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:. 
//If you override, you MUST call super.

示例如下,不要忘记加 super:

- (void)prepareForReuse
{
    [super prepareForReuse];
    
    self.sellPriceLabel.text=@"";
    self.sellNumLabel.text=@"";
}

相关文章

网友评论

      本文标题:iOS tableView 复用自定义的 cell 导致的数据错

      本文链接:https://www.haomeiwen.com/subject/hqrhpqtx.html