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

2019-03-07  本文已影响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=@"";
}
上一篇 下一篇

猜你喜欢

热点阅读