2014-06-10 40 views
0

我的單元格正在將數據混入cellforrowatindexpath中。我更改標籤的文本顏色和文本,但即使isComplete值發生更改,單元格顏色和文本也會隨機更改。cellforrowatindexpath在單元格中混合數據

例如,當我滾動瀏覽表格視圖時,完成的任務有時會變成紅色。奇怪的是,這隻發生在我滾動的單元格上,而不是最初加載並在屏幕上可見的單元格。

這裏是我的方法是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TaskCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:@"TaskCell"]; 
    PFObject *player = (_tasks)[indexPath.row]; 

    NSString *taskType = player[@"name"]; 
    NSNumber *completeNum = player[@"complete"]; 
    BOOL isComplete = [completeNum boolValue]; 


    UIImage *completeImage; 
    NSLog(@"task: %@ is complete %@", player[@"taskDescription"], player[@"complete"]); 
    if(isComplete){ 
     completeImage = [UIImage imageNamed:@"check"]; 
     cell.taskDescriptionLabel.text = player[@"taskDescription"]; 
    }else{ 
     completeImage = [UIImage imageNamed:@"x2"]; 
     cell.taskDescriptionLabel.text = @"Incomplete"; 
     cell.taskDescriptionLabel.textColor = [UIColor redColor]; 
    } 

    cell.taskCompletedImage.image = completeImage; 


    cell.TaskTypeLabel.text = taskType; 

    return cell; 
} 

爲什麼會出現這種情況?

+0

代碼對我來說看起來沒問題。引用數組(_tasks)有點奇怪。這是一個屬性?如何self.tasks?另外,你爲什麼要將任務數組中的元素稱爲「玩家」?但沒有一個解釋了這個問題。 – danh

+0

你的問題實際上在於你如何設置'player [@「complete」]屬性。請提供你如何設置的代碼。 – jakenberg

回答

1

假設該單元格被重用,當使用具有紅色的單元格的重用實例時,您會看到紅色。通過重寫prepareForReuse方法來重置顏色。或者每次單元格配置如下時重置顏色。

if(isComplete){ 
    completeImage = [UIImage imageNamed:@"check"]; 
    cell.taskDescriptionLabel.text = player[@"taskDescription"]; 
    cell.taskDescriptionLabel.textColor = [UIColor blackColor]; 
}else{ 
    completeImage = [UIImage imageNamed:@"x2"]; 
    cell.taskDescriptionLabel.text = @"Incomplete"; 
    cell.taskDescriptionLabel.textColor = [UIColor redColor]; 
} 
相關問題