Button in UITableViewCell not responding under ios 7

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I have one tableview and each cell contains one button. It's working pretty well all iOS versions but 7. I don't know what's going on. The cell is constructed in one xib file.

Could anyone give me some suggestions? Thanks in advance.

回答1:

What worked for me is this:

contentView.userInteractionEnabled = NO; 

called after loading my custom cell from nib. It looks like this content view is created and set as a top-level child view after creating views defined in the nib file.



回答2:

Calling [cell bringSubviewToFront:button] after loading the cell from nib solved this for me.



回答3:

I have also struggled with this problem and I did a lot of research over the different answers on stackoverflow and internet, the solution is different depending on the source of the problem. For the most case, you need to do the following with the element that is supposed to take user action (tap/click etc.):
myButton.userInteractionEnabled = YES;

Particularly for UILabel/UIImageView, by default the userInteractionEnabled flag is NO.

If you are loading the view from a xib, you need make sure, from the custom cell view, content view, and the element you want the user action enabled, all have the check box checked:

In the example above, DeviceViewCell, Content View and Button element all have "User Interaction Enabled" check box checked.
If the custom cell is implemented as a sub class of UITableViewCell, and the custom cell view is loaded from another xib file dynamically, you HAVE TO DO one more thing which is confusing to most of the people but I will explain why later on:
In the custom cell's initialization method, you need to do the following:
self.contentView.userInteractionEnabled = NO

where self is the subclass of UITableViewCell you created. Again, IF the custom cell is implemented as a sub class of UITableViewCell, and the custom cell view is loaded from another xib file DYNAMICALLY, you need to do the above. Here is the explanation:

self.contentView is NOT the same Content View you see in Interface Builder, it is created by the system as the default content view that is on TOP of every other views/elements within your custom cell view, and it obscures the rest of views in the view hierarchy. Hence, you will need to disable the user interaction for it before the UI elements in the custom cell view can receive user interaction events. Hope this helps. If you have a better explanation I would love to hear it :-). Again is is my observation and there could be other reasons. Discussion is welcome!



回答4:

The problem is that your nib is not containing a contentView, as required since the iOS 8 SDK (I'm writing by experience, don't have a viable source, sorry).

When you add an UITableViewController to a storyboard, an UITableViewCell is added to it's UITableView automatically. If you take a look at this UITableView, you'll see a contentView in it.

So when you subclass an UITableViewCell and create it with a .xib, you'll have to click&drag a UITableViewCell to the xib instead of the default UIView. Then don't forget to set the File Owner class and the UITableViewCell class to your subclass.



回答5:

I experienced the same issue. It turns out I had created a UIView object in IB and then set it's class to a subclass of UITableViewCell.

Since the object I originally created in IB was a UIView, the UITableViewCell contentView didn't get added until runtime, which happened to be on top of my UIButtons making them un-selectable.



回答6:

Clearly, there are a number of things that can go wrong and some fixes work on certain iOS versions and not on others.

My problem was that I had a UIButton that was not an immediate subview of cell.contentV

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!