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

查看:110
本文介绍了选择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.

如何在选择tableView时显示我的UIView backgroundColor?

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

推荐答案

这里的问题是[超级]实现

The problem here is that the [super] implementation of

- (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. setHighlighted

  2. touchesEnded

  3. layoutSubviews

  4. willSelectRowAtIndexPath(委托方)

  5. setSelected(!!! this是所有视图背景颜色被告知消失的地方)

  6. didSelectRowAtIndexPath(委托方)

  7. setSelected(再次)(有趣的是背景颜色未被清除这个电话。超级方法里面有什么奇怪的感觉?)

  8. layoutSubviews(再次)

  1. setHighlighted
  2. touchesEnded
  3. layoutSubviews
  4. willSelectRowAtIndexPath (delegate side)
  5. setSelected (!!! this is where all your view background colors are told to disappear)
  6. didSelectRowAtIndexPath (delegate side)
  7. setSelected (again) (Interestingly background colors not cleared on this call. What strangeness is going on inside that super method?)
  8. layoutSubviews (again)

所以你的选择是


  1. 覆盖 - (void)setSelected:(BOOL)选择动画:(BOOL)动画; ,不调用 [super setSelected:selected animated: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天全站免登陆