Question

I use Core Data and MagicalRecord to manage items, which can be enabled or disabled. There is an enabled flag on the Item entity in the Data Store. An item also has a title. Finally, I display the items in a table view using an NSFetchedResultsController. The table view has two sections: The first one is for enabled items and the second one is for disabled items. All items are enabled by default. The cell of a disabled item has a different background color (yellow). To complicate things a bit more, cells are loaded from a nib file like so:

  - (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];
      }
  }

  // ...

Now, when I disable an item, delete it, then create a new item with the same title, the new item's cell has a yellow background even though the new item is enabled. If I check the item itself, it is indeed enabled, so it's just the cell that stays yellow.

Does anybody know what the problem might be please?

Was it helpful?

Solution

This is a common mistake.

You are dequeueing a reusable cell. It will be in whatever state it was when it was added to the queue (or cache, if you prefer).

You need to add an else block of code in the configureCell: method:

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

OTHER TIPS

You need to set default background color. It happens because after dequeuing cell, background color doesn't set automatically. So just make it so:

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top