UITableViewCell 中的按钮在 ios 7 下没有响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18848689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:16:45  来源:igfitidea点击:

Button in UITableViewCell not responding under ios 7

iosuitableviewuibuttonios7

提问by Xavier

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.

我有一个 tableview,每个单元格包含一个按钮。它在所有 iOS 版本中都运行良好,但 7。我不知道发生了什么。该单元在一个 xib 文件中构建。

Could anyone give me some suggestions? Thanks in advance.

谁能给我一些建议?提前致谢。

回答by b005t3r

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.

从笔尖加载我的自定义单元格后调用。在创建在 nib 文件中定义的视图之后,看起来这个内容视图被创建并设置为顶级子视图。

回答by yonix

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

[cell bringSubviewToFront:button]从nib加载单元格后调用为我解决了这个问题。

回答by us_david

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;

我也遇到过这个问题,我对 stackoverflow 和 internet 上的不同答案做了很多研究,解决方案因问题的来源而异。在大多数情况下,您需要对应该执行用户操作(点击/单击等)的元素执行以下操作:
myButton.userInteractionEnabled = YES;

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

特别是对于 UILabel/UIImageView,默认情况下userInteractionEnabled标志是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:
enter image description here

如果您正在从 xib 加载视图,您需要确保从自定义单元格视图、内容视图和您希望启用用户操作的元素都选中复选框:
在此处输入图片说明

In the example above, DeviceViewCell, Content Viewand Buttonelement 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

在上面的示例中,DeviceViewCellContent ViewButton元素都选中了“启用用户交互”复选框。
如果自定义单元格作为一个子类的UITableViewCell的实施,以及自定义单元格视图从另一个厦门国际银行文件动态加载,你必须做的一两件事,这是混淆于大多数人,但为什么后来我将解释的:
在自定义单元格的初始化方法,需要做如下操作:
self.contentView.userInteractionEnabled = NO

where selfis 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您创建的 UITableViewCell 的子类在哪里。同样,如果自定义单元格是作为 UITableViewCell 的子类实现的,并且自定义单元格视图是从另一个 xib 文件动态加载的,则需要执行上述操作。这是解释:

self.contentViewis NOT the same Content Viewyou see in Interface Builder, it is created by the system as the default content viewthat 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!

self.contentViewContent View您在 Interface Builder 中看到的不同,它是由系统创建的,因为default content view它位于自定义单元格视图中所有其他视图/元素的顶部,并且它掩盖了视图层次结构中的其余视图。因此,在自定义单元格视图中的 UI 元素可以接收用户交互事件之前,您需要为其禁用用户交互。希望这可以帮助。如果您有更好的解释,我很想听听:-)​​。再次是我的观察,可能还有其他原因。欢迎讨论!

回答by dulgan

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).

问题是您的笔尖不包含自 iOS 8 SDK 以来所要求的 contentView(我是根据经验编写的,没有可行的来源,抱歉)。

When you add an UITableViewControllerto a storyboard, an UITableViewCell is added to it's UITableViewautomatically. If you take a look at this UITableView, you'll see a contentViewin it.

当您UITableViewController向故事板添加 时,会UITableView自动向其中添加 UITableViewCell 。如果你看一下这个UITableView,你会看到里面有一个contentView

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

因此,当您子类化 anUITableViewCell并使用 .xib 创建它时,您必须单击并拖动 aUITableViewCell到 xib 而不是默认的UIView。然后不要忘记将 File Owner 类和UITableViewCell类设置为您的子类。

回答by mikejonesguy

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

很明显,有很多事情可能会出错,一些修复程序适用于某些 iOS 版本,而不适用于其他版本。

My problem was that I had a UIButton that was not an immediate subview of cell.contentView (i.e. it was a subview of a subview). I had to add my button directly to cell.contentView in order for it to be tappable. (iOS v8+)

我的问题是我有一个 UIButton,它不是 cell.contentView 的直接子视图(即它是子视图的子视图)。我必须将我的按钮直接添加到 cell.contentView 以便它可以被点击。(iOS v8+)

回答by Collin

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.

我遇到了同样的问题。原来我在 IB 中创建了一个 UIView 对象,然后将它的类设置为 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.

由于我最初在 IB 中创建的对象是 UIView,因此直到运行时才添加 UITableViewCell contentView,这恰好位于我的 UIButton 之上,使它们无法选择。

回答by Muhammad Arqam Owais

The solution to this problem is;

这个问题的解决方法是;

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    
cell.bringSubviewToFront(cell.YOUR_UIView)    
}

This works for me.

这对我有用。

回答by Adrian Silveanu

My Cell's outline

我的细胞轮廓

In my case, everything that I would like the user to interact with is outside of the ContentView. Also I do not want the cell to be highlighted when it is selected.

就我而言,我希望用户与之交互的所有内容都在 ContentView 之外。此外,我不希望单元格在被选中时突出显示。

All labels (Name, Categories, etc.) are immediately under "Content View". The WebsiteButton is not under "Content View", but it is under "Local Directory Table Cell".

所有标签(名称、类别等)都直接位于“内容视图”下。WebsiteButton 不在“内容视图”下,而是在“本地目录表单元格”下。

In my tableViewController method tableView:cellForRowAtIndexPath: I have the following code: cell.contentView.userInteractionEnabled = NO;

在我的 tableViewController 方法 tableView:cellForRowAtIndexPath: 我有以下代码: cell.contentView.userInteractionEnabled = NO;

That is all and the user is able to interact with the button. I do NOT use [cell bringSubviewToFront:cell.websiteButton];

这就是全部,用户可以与按钮进行交互。我不使用 [cell becomeSubviewToFront:cell.websiteButton];

回答by Snail

I have been get the same problem. The reason is

我一直遇到同样的问题。原因是

self.contentView.Frame.size.height

is always 44.

总是 44。

There is a suggestion you can set your self.contentView.Frameto self.frameat the overwrite method - (void)layoutSubviewsthe problem will be solved

还有就是你可以设置你的建议self.contentView.Frame,以self.frame在重写方法-(void)layoutSubviews这个问题将得到解决

回答by diatche

As others have pointed out, always add subviews to the cell's content view. If you have overridden layoutSubviewsmethod, make sure to call the super implementation. Forgetting to call the super implementation yields unexpected results.

正如其他人指出的那样,始终将子视图添加到单元格的内容视图中。如果您有重写的layoutSubviews方法,请确保调用超级实现。忘记调用 super 实现会产生意想不到的结果。