不能在iOS6中设置UITableViewCell的背景颜色?
我开始在模拟器下testing我的应用程序,因为我没有任何iOS 6设备,偶然发现了奇怪的问题。 我无法设置UITableViewCell的backgroundColor属性。 如果我这样说:
cell.contentView.backgroundColor = [UIColor redColor];
它只适用于iOS 6,当我使用这个:
cell.backgroundColor = [UIColor redColor];
或这个
[cell setBackgroundColor:[UIColor redColor]];
它只适用于iOS7。
当我使用cell.contentView
和cell.backgroundColor
这两个iOS的工作…不应该是这样一个“简单”属性的答案吗? 或者,也许这是一些模拟器的错误?
更新:如果它改变任何东西在同一个tableview和单元格我不能设置accessoryType
既不通过StoryBoard也不代码…
更新2:由于某种原因设置tableview样式,以纯平删除所有我的更改,但分组显示为预期…
在iOS6中,您需要更改willDisplayCell中单元格的backgroundColor。 这也适用于iOS7,不需要特定于版本的代码。
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor redColor]]; }
我没有看到设置contentView的背景和直接到单元格的问题。 iOS 7已经改变了很多这个类。 如果你需要与旧系统兼容,那么你需要做这种types的事情。
所以是的,你应该使用两个:
cell.contentView.backgroundColor cell.backgroundColor
尝试改变单元格的backgroundView颜色而不是backgroundColor。
UIView *myView = [[UIView alloc] init]; myView.backgroundColor = [UIColor whiteColor]; cell.backgroundView = myView;
如果你设置(假设你使用标准单元):
cell.textLabel.backgroundColor = [UIColor clearColor];
…那么你只需要使用:
cell.contentView.backgroundColor = [UIColor redColor];
iOS 6似乎将表格的背景颜色复制到textLabel。
尝试添加此代码:
cell.backgroundView.backgroundColor = [UIColor clearColor];