选择 UITableViewCell 时 UIView backgroundColor 消失 [英] UIView backgroundColor disappears when UITableViewCell is selected

查看:31
本文介绍了选择 UITableViewCell 时 UIView backgroundColor 消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在界面构建器中构建了一个简单的 tableViewCell.它包含一个包含图像的 UIView.现在,当我选择单元格时,会显示默认的蓝色选择背景,但我的 UIView 的 backgroundColor 消失了.

I have a simple tableViewCell build in interface builder. It contains a UIView which contains an image. Now, when I select the cell, the default blue selection background is shown, but the backgroundColor of my UIView is gone.

我的 UITableViewCell 的实现文件没有做任何特别的事情.它只是初始化的 &返回 self 而我在 setSelected 中所做的就是调用 super.

My UITableViewCell's implementation file doesn't do anything special. It just init's & returns self and all I do in setSelected is call super.

如何让我的 UIView backgroundColor 在 tableView 被选中时显示?

How do I get my UIView backgroundColor to show when the tableView is selected?

推荐答案

这里的问题是

- (void) setSelected:(BOOL) selected animated:(BOOL) animated;

将 UITableViewCell 中的所有背景颜色设置为 rgba(0,0,0,0).为什么?也许让我们都流汗?

sets all the background colors in the UITableViewCell to rgba(0,0,0,0). Why? Perhaps to make us all sweat?

并不是整个视图都消失了(事实证明,如果您更改视图图层边框属性,这些属性将被保留)

It is not that entire views disappear (as evidenced by the fact that if you change the views layer border properties, those are retained)

这是触摸单元格产生的函数调用序列

Here is the sequence of function calls that results from touching a cell

  1. 设置高亮
  2. touchesEnded
  3. layoutSubviews
  4. willSelectRowAtIndexPath(委托方)
  5. setSelected(!!!这是你所有视图背景颜色被告知消失的地方)
  6. didSelectRowAtIndexPath(委托方)
  7. setSelected(再次)(有趣的是,这次调用没有清除背景颜色.那个超级方法内部发生了什么奇怪的事情?)
  8. layoutSubviews(再次)

所以你的选择是

  1. 覆盖- (void) setSelected:(BOOL) selected Animation:(BOOL)animated; 不调用[super setSelected:selected animation:animated].这将为您提供技术上最正确的实现,因为 a) 代码包含在 UITableViewCell 子类中,并且 b) 因为它只在需要时调用(需要时调用两次,但也许有办法解决这个问题).不利的一面是您必须重新实现 setSelected 的所有必要功能(而不是不必要的颜色清除功能).现在不要问我如何正确覆盖 setSelected .目前您的猜测和我的一样好(请耐心等待,我会在弄清楚后编辑此答案).
  2. didSelectRowAtIndexPath 中重新声明背景颜色.这不是很好,因为它把应该是实例代码的东西放在实例之外.它的优点是仅在需要时才调用它,而不是 ...
  3. layoutSubviews 中重新声明背景颜色.这一点都不好,因为 layoutSubviews 被调用了一百万次!每次表格刷新,每次滚动,每次你祖母烫发时都会调用它......就像认真地,一百万次.这意味着有很多不必要的后台重新断言和很多额外的处理开销.从好的方面来说,它将代码放在 UITableViewCell 子类中,这很好.
  1. Override - (void) setSelected:(BOOL) selected animated:(BOOL) animated; without calling [super setSelected:selected animated:animated]. This will give you the most technically correct implementation because a) the code is wrapped up inside the UITableViewCell subclass and b) because it is only called when needed (well twice when needed, but maybe there is a way around that). The down side is you'll have to re-implement all the necessary functions (as opposed to unnecessary color clearing functions) of setSelected. Now don't ask me how to properly override setSelected just yet. Your guess is as good as mine for now (be patient, I'll edit this answer once I figure it out).
  2. Re-assert the background colors in didSelectRowAtIndexPath. This is not so great because it puts what should be instance code outside the instance. It has the upside that it is only called when it is needed, as opposed to ...
  3. Re-assert the background colors in layoutSubviews. This is not great at all because layoutSubviews is called like A MILLION times! It is called every time the table refreshes, every time it scrolls, every time you grandmother gets a perm... like seriously, a million times. That means there is a lot of unnecessary background re-assertions and a lot of extra processing overhead. On the bright side it puts the code inside the UITableViewCell subclass, which is nice.

不幸的是,在 setHighlighted 中重新断言背景颜色没有任何作用,因为在第一次调用 setSelected 将所有背景颜色设置为 [r:0 b:0 g:0 a:0] 之前调用 setHighlighted.

Unfortunately re-asserting the background colors in setHighlighted does nothing because setHighlighted is called before all the background colors get set to [r:0 b:0 g:0 a:0] by the first call to setSelected.

//TODO:详细说明如何覆盖 setSelected(敬请期待)

//TODO: Give a great description of how to override setSelected (stay tuned)

这篇关于选择 UITableViewCell 时 UIView backgroundColor 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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