2

我正在从 UITableViewController 中的 nib 文件加载 UITableView 单元格,并且未显示我在 Interface Builder 中设置的背景颜色。为什么不?

4

1 回答 1

2

您应该使用Apple 在Table View Programming Guide for iOStableView:willDisplayCell:forRowAtIndexPath:中推荐的设置背景颜色:

tableView:willDisplayCell:forRowAtIndexPath:表格视图在绘制行之前向其委托发送消息。如果委托选择实现此方法,它可以在单元格对象显示之前对其进行最后一分钟的更改。使用此方法,委托应该只更改之前由表视图设置的基于状态的属性,例如选择和背景颜色,而不是内容。

此代码执行交替的“斑马条纹”效果:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.1];
        cell.backgroundColor = altCellColor;
    }
}
于 2013-04-24T00:44:51.493 回答