UITableViewCell的背景颜色
我正在使用IB中设置的基本表格视图单元格。 我应用的唯一的自定义是在cellForRowAtIndexPath,在那里我只是设置单元格的标签的背景颜色( 不是单元格本身的颜色)。
[[cell textLabel] setBackgroundColor:[UIColor yellowColor]];
第一次绘制表格时,不应用背景颜色。 当我向下滚动时,它被应用到绘制的新单元格中。 同样,如果向后滚动,则顶部单元格将以正确的背景色重新绘制。
这背后是什么解释?
编辑 – 这是我的cellForRowAtIndexPath,并在IB中设置:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell2"]; [[cell textLabel] setBackgroundColor:[UIColor yellowColor]]; return cell;
}
你必须使用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
从文档:
表视图在它使用单元格绘制行之前将其发送给它的委托,从而允许委托在显示之前自定义单元对象。 此方法使委托有机会覆盖表视图较早设置的基于状态的属性,如select和背景颜色。