3

我有一个 uiTableView。我希望每个其他单元格都获得另一种背景颜色。我正在尝试这样。但它不会起作用。有人有线索吗?

 if(indexPath.row % 2 == 0){
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColor blueColor];
    }

亲切的问候,

史蒂夫

4

3 回答 3

7

您可以使用 :cell.contentView.backgroundColor而不是cell.backgroundColorinsidetableView:cellForRowAtIndexPath:

或者:(来自 Apple 文档)

注意:如果要更改单元格的背景颜色(通过 UIView 声明的 backgroundColor 属性设置单元格的背景颜色),您必须在委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行,而不是在 tableView :cellForRowAtIndexPath: 数据源的。更改组样式表视图中单元格的背景颜色在 iOS 3.0 中产生的效果与以前版本的操作系统不同。它现在影响圆角矩形内部的区域,而不是它外部的区域。

于 2012-10-02T08:30:22.553 回答
0

要回答评论中的扩展问题,请将标签的背景颜色设置为label.backgroundColor = [UIColor clearColor];

于 2012-10-02T10:00:16.687 回答
0
cell.backgroundView=[[UIView alloc]initWithFrame:cell.frame];
[cell.backgroundView setBackgroundColor:[UIColor blueColor]];

会做这项工作或

[cell.contentView setBackgroundColor:[UIColor blueColor]];
于 2012-10-02T08:17:41.987 回答