2014-06-10 39 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]; 
} 
相关问题