I have made a custom UITableViewCell called "SwitchCell" that has a switch. In iOS9 Only, using Xcode 7 beta, the Content view in the cell is on top of the switch. (See screenshot of View Hierarchy. You can clearly see that the content view of the cell is on top of the other views. ):
So all the touches to the UISwitch are intercepted, and the IBAction does not fire. In iOS8, this is not a problem. See screenshot for iOS 8.4 simulator. You can see that there is no content view on top of the controls:
Has anyone had this problem? I tried remaking the NIB from scratch, but the same result occurs.
My NIB is a freeform size view with No status bar. It has two outlets: one for UILabel, one for UISwitch.
EDIT: please make sure to check the answer below that asks to verify that the cell's root view is not just a UIView but a UITableViewCell. This issue may also be a side effect of this.
答案 0 :(得分:12)
After more investigation and searching, i found my solution here: Button in UITableViewCell not responding under ios 7
What fixed it for me was: cell.contentView.userInteractionEnabled = NO;
This prevents the cell content view from taking over the touch events, even though it's on top of the other views.
This issue was not only happening on iOS9, but on iOS7 as well. In iOS8, the Content view was behind the controls.
答案 1 :(得分:4)
问题可能出在您单元格的.xib文件中。在单独的.xib中创建单元格时,请务必在画布上拖动 UITableViewCell ,而不是UIView 。
答案 2 :(得分:2)
当我将单元格重用为.xib时,我遇到了这个问题。我没有意识到,但是当我第一次创建xib时,它创建的默认视图实际上是UIView
而不是UITableViewCell
。似乎在某个阶段,UIKit会在我的其他元素之上添加内容视图,从而中断某些事件(例如触摸事件)。
我通过打开我的xib文件并将UITableViewCell
拖到画布上并将我的UI元素从旧视图复制到新单元格来解决此问题。
之后,属性检查器中的其他设置也可用于匹配UITableViewCell
的设置。
答案 3 :(得分:2)
SWIFT
我遇到一个问题,自定义tableviewcell swift文件中的按钮可以正常工作,但是后来我升级到Xcode 12,突然之间我无法访问它们(这意味着我的水龙头无法识别) 。单元格内容视图似乎干扰了层次结构,这就像救了我一样:
cell.contentView.isUserInteractionEnabled = false
我将这一行放在cellForRowAt中。
谢谢@FranticRock