改变cell.accessoryView和cell.editingAccessoryView的背景颜色 [英] Altering the background color of cell.accessoryView and cell.editingAccessoryView

查看:142
本文介绍了改变cell.accessoryView和cell.editingAccessoryView的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我将一个accessoryView添加到我的UITableViewCell时,它都没有背景颜色?我正在设置一个UISwitch作为我的accessoryView,我在cell.backgroundColor属性中设置的颜色只影响contentView而不影响accessoryView。
我已经尝试了将它们设置为相同值的所有内容。我试图将cell.backgroundView.backgroundColor和cell.accessoryView.backgroundColor属性设置为我想要的颜色,但没有任何工作。我还尝试在contentView中创建一个子视图,它解决了backgroundColor问题(通过避免它),但它产生了问题,当文本太长时,开关位于cell.textLabel的顶部。

Whenever I add an accessoryView to my UITableViewCell, it doesn't carry the background color across? I'm setting a UISwitch as my accessoryView, and the color I have set in the cell.backgroundColor property only effects the contentView and not the accessoryView. I have tried everything to set them to the same value. I tried to set the cell.backgroundView.backgroundColor and the cell.accessoryView.backgroundColor properties to the color I want but nothing is working. I also tried creating a subview inside contentView, which solved the backgroundColor problem (by avoiding it), but it creates the problem, where the switch sits on top of the cell.textLabel when the text is too long.

我是否有办法在不在contentView中创建子视图的情况下修改accessoryView的背景颜色,或者在没有子类化UITableViewCell的情况下改变cell.textLabel的长度?

Is there are way I can modify the background color of the accessoryView without creating a subview in contentView, or to alter the length of the cell.textLabel without subclassing UITableViewCell?

推荐答案

在阅读文档(一个新颖的想法)后,我发现了一篇文章仔细查看表视图单元格。它帮助我理解了细胞的组成,我找到了答案...

Upon reading the documentation (a novel idea), I found the article, "A Closer Look at Table-View Cells". It helped me understand the composition of the cells, and I found my answer...

细胞看起来像这样......

cells look like this...

自从cell.accessoryView是cell.contentView的姐妹视图我不得不向cell.contentView询问其superview,然后我能够一次更改两个视图的背景颜色。这是代码的样子......

Since the cell.accessoryView is a sister view to cell.contentView I had to ask the cell.contentView for its superview, and then I was able to change the background color for both views at once. Here's what the code looks like...

// Cell Formatting
cell.contentView.superview.backgroundColor = [UIColor greenColor];

我知道这很简单,但我是新手,我花了很长时间才放慢脚步阅读文档。希望这有助于其他一些人!

I know it's really simple, but I'm a newbie and it took me ages to slow down and read the doc. Hopefully, this helps some other folks out there!

这篇关于改变cell.accessoryView和cell.editingAccessoryView的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆