我有一个 uiTableView。我希望每个其他单元格都获得另一种背景颜色。我正在尝试这样。但它不会起作用。有人有线索吗?
if(indexPath.row % 2 == 0){
cell.backgroundColor = [UIColor whiteColor];
}else{
cell.backgroundColor = [UIColor blueColor];
}
亲切的问候,
史蒂夫
我有一个 uiTableView。我希望每个其他单元格都获得另一种背景颜色。我正在尝试这样。但它不会起作用。有人有线索吗?
if(indexPath.row % 2 == 0){
cell.backgroundColor = [UIColor whiteColor];
}else{
cell.backgroundColor = [UIColor blueColor];
}
亲切的问候,
史蒂夫
您可以使用 :cell.contentView.backgroundColor
而不是cell.backgroundColor
insidetableView:cellForRowAtIndexPath:
或者:(来自 Apple 文档)
注意:如果要更改单元格的背景颜色(通过 UIView 声明的 backgroundColor 属性设置单元格的背景颜色),您必须在委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行,而不是在 tableView :cellForRowAtIndexPath: 数据源的。更改组样式表视图中单元格的背景颜色在 iOS 3.0 中产生的效果与以前版本的操作系统不同。它现在影响圆角矩形内部的区域,而不是它外部的区域。
要回答评论中的扩展问题,请将标签的背景颜色设置为label.backgroundColor = [UIColor clearColor];
cell.backgroundView=[[UIView alloc]initWithFrame:cell.frame];
[cell.backgroundView setBackgroundColor:[UIColor blueColor]];
会做这项工作或
[cell.contentView setBackgroundColor:[UIColor blueColor]];