iOS UITableView : changing background color of table overrides background color of cells

标签 ios objective-c xcode uitableview

我遇到一个问题,设置 tableView 的背景颜色会覆盖我为各个单元格设置的背景颜色。更具体地说,我有一个包含 3 行的表格:顶部单元格为深灰色,底部 2 个单元格为红色:

/image/Hx9bm.png

- (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 背景颜色就可以做到这一点:

/image/2C1eg.png

tableView.backgroundColor = [UIColor blackColor];

但是,正如您所注意到的,这行代码也将顶部两个单元格的背景颜色更改为黑色。我对此进行了更多的尝试,并注意到一种通用模式,其中每个单元格背景颜色都会更改为黑色,除了最后一个:

(参见上面链接中的第三张图片)

关于这里发生的事情有什么想法吗?帮助将不胜感激!



我已经尝试过的一些方法不起作用(我会提供更多链接,但我当前的代表不允许我这样做):

A) 使单元格中的 View 不透明。更改单元格中不同 View 的颜色。

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];

最佳答案

setting the background color of my tableView overrides the background colors I've set for my individual cells

这是正确的。很烦人,不是吗? :) 问题实际上就是要么给单元格一个彩色的backgroundView(比听起来容易得多),要么等到tableView:willDisplayCell:forRowAtIndexPath:,然后设置那里的单元格的backgroundColor

关于iOS UITableView : changing background color of table overrides background color of cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712445/

相关文章:

iOS 应用程序从后台连接低功耗蓝牙服务

iphone - UITableview 单元格高度在空表中不起作用?

objective-c - GKTurnBasedMatch endTurnWithNextParticipants :turnTimeout:matchData:completionHandler: 问题

ios - Swift 委托(delegate)没有调用

ios - CoreLocation的区域监控系统定义的缓冲是什么?

android - 我需要一个稳定且统一的解决方案来在 Android 和 IOS 网络浏览器中流式传输音频

objective-c - 如果 NSString 不等于函数?

ios - Instruments Allocations 跟踪用户定义类的对象的分配和解除分配

objective-c - 为 UIBarButton 创建自定义按钮,选择器抛出异常

ios - 以 AAC 格式录制的 xCode 无法正常工作