我开始在模拟器下测试我的应用程序,因为我没有任何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样式为plain删除了我的所有更改,但分组显示为预期...
在iOS6中,您需要在willDisplayCell中更改单元格的backgroundColor.这也适用于iOS7,无需特定于版本的代码.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor redColor]]; }
我没有看到将contentView AND的背景设置为单元格的问题.iOS 7已经改变了很多这个类.如果您需要与旧系统兼容,那么您需要执行此类操作.
所以是的,你应该同时使用:
cell.contentView.backgroundColor cell.backgroundColor