在iOS7中更改tableview单元格的背景颜色

时间:2013-09-30 16:42:42

标签: ios uitableview ios7

我遇到了自升级到iOS7后出现的问题,当我尝试更改特定tableview单元格的背景颜色时,它没有为正确的单元格添加颜色(通常是指定的单元格除了其他单元格) 。从下面的代码中可以看出,我定义了我想要突出显示的类型,然后更改颜色。它在iOS升级之前完美运行,所以我不确定导致这种情况发生了什么变化:

快速编辑:此外,当我向下滚动桌面视图然后再向上时,它会在桌面视图控制器第一次加载时为未着色的单元格着色(如果这有帮助的话)。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
    if ([type isEqualToString:@"ace"]){
        cell.backgroundColor = [UIColor colorWithRed:0.81 green:0.91 blue:0.81 alpha:1.0];
    }
}

2 个答案:

答案 0 :(得分:4)

我认为在tableView: cellForRowAtIndexPath:方法中进行单元格自定义更好。在这种方法中,

if ([type isEqualToString:@"ace"])
{
    cell.backgroundColor = [UIColor aceColor];
}
else // this else is important. If you add this, scrolling works fine.
{
    cell.backgroundColor = [UIColor otherCellColor];
}

答案 1 :(得分:0)

您可能只有一个可重复使用的单元格样式。考虑为您的Aces提供可重复使用的单元格样式,并为所有其他样式设置一个。在cellForRowAtIndexPath中设置背景颜色,而不是willDisplayCell。

伪码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
. 
 NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
 if ([type isEqualToString:@"ace"]){
 {
  // load a cell with the background color desired
  cell = 
  cell.backgroundColor = 
  .
  .
  .
  return (cell);
 }

 // else a normal cell
 cell = 
 .
 .
 .
}