2014-04-10 151 views
0

我使用的核心數據和MagicalRecord管理項目,它可以是啓用禁用。有 和已啓用標記項目實體在Data Store中。一個項目也有一個標題。最後,使用NSFetchedResultsController,我在 的表格視圖中顯示項目。表格視圖有兩個 部分:第一個用於啓用項目,第二個用於禁用項目。全部項目默認啓用禁用項目的單元具有不同的背景顏色(黃色)。 要多一點複雜的事情,細胞從榫文件加載像這樣:的UITableViewCell從筆尖沒有更新加載(緩存?)

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

    // ... 

現在,當我禁用項目刪除,然後創建項目具有相同標題,則 新項目的細胞有一個黃色背景,即使新項目啓用。如果我檢查項目 本身,它確實啓用,所以它只是保持黃色的單元格。

有人知道這個問題可能是什麼嗎?

回答

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 
    } 
} 
+0

啊!來自我的愚蠢的錯誤...非常感謝!目前無法接受答案,您發佈速度太快! –

0

您需要設置默認背景色。 發生這種情況是因爲在將單元格出隊後,背景顏色不會自動設置。 所以就這樣吧:

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