【发布时间】:2013-03-29 22:33:51
【问题描述】:
我有一个问题,我的 tableView 的背景颜色会覆盖我为单个单元格设置的背景颜色。更具体地说,我有一个包含 3 行的表格:顶部单元格为深灰色,底部 2 个单元格为红色:
http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#0
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *sectionBackgroundColor = [UIColor colorWithRed:0.09f green:0.09f blue:0.09f alpha:1.0];
UIColor *menuItemBackgroundColor = [UIColor redColor];
if (indexPath.row == 0) {
cell.backgroundColor = sectionBackgroundColor;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = @"MENU";
cell.textLabel.font= [UIFont systemFontOfSize:16.0];
cell.selectionStyle = false;
} else if (indexPath.row == 1) {
cell.backgroundColor = menuItemBackgroundColor;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = @"Schedule";
cell.textLabel.font= [UIFont systemFontOfSize:18.0];
cell.imageView.image = [UIImage imageNamed:@"Schedule.png"];
} else if (indexPath.row == 2) {
cell.backgroundColor = menuItemBackgroundColor;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = @"Contacts";
cell.textLabel.font= [UIFont systemFontOfSize:18.0];
cell.imageView.image = [UIImage imageNamed:@"Contacts.png"];
} else {
cell.backgroundColor = [UIColor blackColor];
cell.selectionStyle = false;
}
}
我正在尝试将底部所有那些空的、未使用的白色单元格设为黑色。根据我的研究,设置 tableView 背景颜色就可以做到这一点:
http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#1
tableView.backgroundColor = [UIColor blackColor];
但是,正如您所注意到的,这行代码也将顶部两个单元格的背景颜色更改为黑色。我对此进行了更多尝试,并注意到了一种通用模式,即除了最后一个之外,每个单元格背景颜色都将更改为黑色:
(见上面链接中的第三张图片)
关于这里发生的事情有什么想法吗?帮助将不胜感激!
我已经尝试过的一些方法不起作用(我会提供更多链接,但我目前的代表不允许我这样做):
A) 使单元格中的视图不透明。更改单元格中不同视图的颜色。
cell.opaque = YES;
cell.backgroundView.opaque = YES;
cell.contentView.opaque = YES;
cell.backgroundColor = menuItemBackgroundColor;
cell.contentView.backgroundColor = menuItemBackgroundColor;
cell.backgroundView.backgroundColor = menuItemBackgroundColor;
B) 设置tableView的backgroundView为nil。
tableView.backgroundView = nil;
C) 设置tableView的backgroundView的背景颜色。
tableView.backgroundView.backgroundColor = [UIColor blackColor];
【问题讨论】:
-
backgroundView默认为nil,所以cell.backgroundView.backgroundColor = menuItemBackgroundColor;除非您添加了背景视图,否则不会执行任何操作。
标签: ios objective-c xcode uitableview