UITableViewCell背景颜色?
我对此进行了相当多的谷歌搜索,但找不到正确的解决方案。我有一个 UITableView,我想更改单元格背景的颜色。我已经找到了执行此操作的解决方案,但它只处理具有内容或数据的单元格。
我还需要为那些未使用的单元格的背景着色。
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试设置 tableView 本身的背景颜色。底部的那些“空单元格”根本不是真正的单元格 - 它们只是在背景上绘制的分隔线。
Try setting the background colour of the tableView itself. Those "empty cells" at the bottom aren't really cells at all - they're just separator lines drawn over the background.
即使 UITableViewCell 继承自 UIView,更改单元格本身的 backgroundColor 属性不会执行任何操作。您需要更改单元格 contentView 的背景颜色,例如
这是因为 UITableViewCell 的子视图实际上是单元格 contentView 的子视图,因为如果单元格进入编辑模式,contentView 知道如何调整其子视图的大小;细胞本身不知道如何做到这一点。
我不确定你所说的未使用的细胞是什么意思。如果您告诉 tableView 有 10 个单元格,而您只为其中 8 个单元格提供内容,那么您仍然会有 10 个绿色单元格(如果这就是您所说的“未使用”的意思)。
Even though UITableViewCell inherits from UIView, changing the backgroundColor property of the cell itself won't do anything. You need to change the background color of the cell's contentView, such as
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; 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".