简体   繁体   English

UITableViewCell背景颜色?

[英]UITableViewCell Background Color?

I have done a fair bit of googling on this and I can't find the right solution. 我已经做了相当多的谷歌搜索,我找不到合适的解决方案。 I have a UITableView of which I want to change the colour of the background of the cells. 我有一个UITableView,我想改变单元格背景的颜色。 I have found solutions to doing this but it only deals with cells which have content or data as it were. 我已经找到了这样做的解决方案,但它只处理具有内容或数据的单元格。

I need to colour the background of those unused cells as well. 我也需要为那些未使用的细胞的背景着色。

Try setting the background colour of the tableView itself. 尝试设置tableView本身的背景颜色。 Those "empty cells" at the bottom aren't really cells at all - they're just separator lines drawn over the background. 底部的那些“空单元格”根本不是真正的单元格 - 它们只是在背景上绘制的分隔线。

Even though UITableViewCell inherits from UIView, changing the backgroundColor property of the cell itself won't do anything. 即使UITableViewCell继承自UIView,更改单元格本身的backgroundColor属性也不会做任何事情。 You need to change the background color of the cell's contentView, such as 您需要更改单元格contentView的背景颜色,例如

cell.contentView.backgroundColor = [UIColor greenColor];

This is because the subviews of a UITableViewCell are actually subviews of the cell's contentView, because the contentView knows how to resize its subviews if a cell is put into editing mode; 这是因为UITableViewCell的子视图实际上是单元格contentView的子视图,因为如果单元格进入编辑模式,contentView知道如何调整其子视图的大小; the cell itself doesn't know how to do that. 细胞本身不知道该怎么做。

I'm not sure what you mean by unused cells. 我不确定未使用的细胞是什么意思。 If you tell your tableView there are 10 cells and you only provide content for 8 of them, you'll still have 10 green cells, if that's what you mean by "unused". 如果你告诉你的tableView有10个单元格而你只为其中8个单元格提供内容,那么你仍然会有10个绿色单元格,如果这就是“未使用”的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM