【问题标题】:UITableViewCell loaded from nib not updated (cached?)从笔尖加载的 UITableViewCell 未更新(缓存?)
【发布时间】:2014-04-10 10:01:27
【问题描述】:

我使用 Core Data 和 MagicalRecord 来管理items,可以启用禁用。有 数据存储中 Item 实体上的 enabled 标志。 item 也有一个title。最后我 使用NSFetchedResultsController 在表格视图中显示items。表视图有两个 部分:第一个用于启用的项目,第二个用于禁用的项目。所有项目 默认情况下启用禁用项的单元格具有不同的背景颜色(黄色)。 更复杂一点的是,单元格是从一个 nib 文件中加载的,如下所示:

  - (void)viewDidLoad
  {
      // ...

      self.items = [Item fetchAllGroupedBy:@"enabled"
                             withPredicate:nil
                                  sortedBy:@"enabled,createdOn"
                                 ascending:NO
                                  delegate:self];
      // ...

      [self.tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil]
           forCellReuseIdentifier:@"CustomCellReuseIdentifier"];

      // ...
  }

  // ...

  - (CustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString     *reuseIdentifier = @"CustomCellReuseIdentifier";
      CustomTableViewCell *cell            =
          (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier
                                                                 forIndexPath:indexPath];

      [self configureCell:cell forRowAtIndexPath:indexPath];

      return cell;
  }

  - (void)configureCell:(CustomTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      Item *item = [self.items objectAtIndexPath:indexPath];

      cell.titleLabel.text = item.title;

      if (!item.enabled.boolValue) {
          cell.backgroundColor = [UIColor colorWithRed:0.999 green:0.895 blue:0.452 alpha:1.000];
      }
  }

  // ...

现在,当我禁用一个item删除它,然后创建一个新的item 同名, 新 item 的单元格 具有黄色背景,即使新 item启用。如果我检查 item 本身,它确实启用,所以它只是保持黄色的单元格。

请问有谁知道问题出在哪里?

【问题讨论】:

    标签: ios core-data nsfetchedresultscontroller magicalrecord


    【解决方案1】:

    这是一个常见的错误。

    您正在使一个可重复使用的单元出队。它将处于添加到队列(或缓存,如果您愿意)时的任何状态。

    您需要在configureCell: 方法中添加else 代码块:

    - (void)configureCell:(CustomTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Item *item = [self.items objectAtIndexPath:indexPath];
    
        cell.titleLabel.text = item.title;
    
        if (!item.enabled.boolValue) {
            cell.backgroundColor = [UIColor colorWithRed:0.999 green:0.895 blue:0.452 alpha:1.000];
        }
        else
        {
            // Set cell.backgroundColor to the enabled color
        }
    }
    

    【讨论】:

    • 啊!我犯的愚蠢错误......非常感谢!还不能接受答案,你发的太快了!
    【解决方案2】:

    您需要设置默认背景颜色。 发生这种情况是因为出列单元格后,背景颜色不会自动设置。 所以就这样吧:

    cell.backgroundColor = [UIColor whiteColor]; //put here default color of your cell
    if (!item.enabled.boolValue) {
              cell.backgroundColor = [UIColor colorWithRed:0.999 green:0.895 blue:0.452 alpha:1.000];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2013-04-27
      • 1970-01-01
      相关资源
      最近更新 更多