UITableViewCellStyleSubtitle标签的BackgroundColor?

P5y*_*cH0 2 background uitableview

我正在尝试创建一个以图像为背景的表格.为实现这一目标,我从背景开始:

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
Run Code Online (Sandbox Code Playgroud)

这导致了一个背景图像,也出现在tablecells中.这不是我想要的东西,所以我试着设置单元格的backgroundColor:

cell.backgroundColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

这根本没有效果!!! 嗯很奇怪.所以我尝试了这个:

UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = backgroundView;
[backgroundView release];
Run Code Online (Sandbox Code Playgroud)

这几乎起作用.剩下的唯一问题是textLabel和detailTextLabel仍然显示它们背后的背景.将这些标签上的backgroundColor设置为白色也没有做任何事情.

我该怎么办?我哪里出错了?我正在尝试实现像worldClock应用程序一样的tableView.

cra*_*erm 10

要改变表格视图单元格的背景颜色,你需要将其设置为tableView:willDisplayCell:forRowAtIndexPath:,而不是tableView:cellForRowAtIndexPath: 否则将不会有任何效果,例如:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor whiteColor];
}
Run Code Online (Sandbox Code Playgroud)