【问题标题】:Duplicate Object when adding in NSMutableArray [iOS]添加 NSMutableArray [iOS] 时重复对象
【发布时间】:2014-03-18 03:03:17
【问题描述】:

您好,我有一个问题,如果我在 uitableview 中添加超过 13 个项目,这些项目会重复

这是我的代码

添加:

[categoryList insertObject:inputcategory atIndex:0];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtindexPaths@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];

CellForRow:

if (cell == nil){
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];
}

行数

return [categoryList count];

前 12 个项目还可以,但之后它在我的 uitableview 中再次在我的 NSMutableArray 中添加相同的最后一个对象,当我重新加载它时,它只记录 12 个项目。

【问题讨论】:

    标签: ios uitableview nsmutablearray


    【解决方案1】:

    cellForRowAtIndexPath:方法改变

     if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];
     }
    

     if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }
     cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];
    

    如果您在if (cell == nil) 中设置文本,则在重复使用单元格时不会更新。重用单元格时不会为nil。

    【讨论】:

    • 哇,谢谢!顺便说一句,这是什么解释?
    • @RonPelayo 值重复,因为 tableview 重用了单元格。因此,当重用单元格不会为 nil 时,这里的内容将不会更新,并且似乎在重复。
    • 感谢@akhilrajtr :D +100000 哈哈
    【解决方案2】:

    Akhilrajtr 的回答是正确的,但这是旧的做法。在 2014 年,对于 iOS 6+,您应该改用 UITableView 方法

    - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
    

    - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
    

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
    

    cellForRowAtIndexPath: 中获取一个单元格变得更加简单...

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    

    "identifier" 是用于标识单元格以供重用的唯一字符串,就像旧方法一样。

    来自 Apple 的文档,“如果现有单元格可用,此方法会使现有单元格出列,或者根据您之前注册的类或 nib 文件创建一个新单元格。”您不再需要使用initWithStyle,如果您认为需要,请考虑继承 UITableViewCell 以更精确地控制单元格的外观。

    不再需要检查单元格是否为零。如果您的单元格是一个普通的 UITableViewCell 或配置您的子类单元格,请继续设置标签(当然,它应该已经准备好使用 prepareForReuse: 方法重用)

    【讨论】:

      【解决方案3】:

      苹果提到了这个in their documentation on UITableViews.的性能

      应该是关于Reuse Cells。所以如果cell != nil 你不能在下面工作:

       if (cell == nil){
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
           cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];  
      }
      

      重复使用单元格。 - 对象分配有性能成本,特别是如果分配必须在短时间内重复发生 - 例如,当用户滚动表视图时。如果您重复使用单元格而不是分配新单元格,则可以大大提高表格视图的性能。

      【讨论】:

      • @RonPelayo 添加这个方法在代码前面:'UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];' 'dequeueReusableCellWithIdentifier' 语句重用单元格。
      猜你喜欢
      • 2013-02-09
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多