简体   繁体   English

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

[英]Button in UITableViewCell not responding under ios 7

I have one tableview and each cell contains one button.我有一个 tableview,每个单元格包含一个按钮。 It's working pretty well all iOS versions but 7. I don't know what's going on.它在所有 iOS 版本中都运行良好,但 7。我不知道发生了什么。 The cell is constructed in one xib file.该单元在一个 xib 文件中构建。

Could anyone give me some suggestions?谁能给我一些建议? Thanks in advance.提前致谢。

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 文件中定义的视图之后,看起来这个内容视图被创建并设置为顶级子视图。

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

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.我也遇到过这个问题,我对 stackoverflow 和 internet 上的不同答案做了很多研究,解决方案因问题的来源而异。 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 .特别是对于 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:如果您正在从 xib 加载视图,您需要确保从自定义单元格视图、内容视图和您希望启用用户操作的元素都选中复选框:
在此处输入图片说明

In the example above, DeviceViewCell , Content View and Button element all have " User Interaction Enabled " check box checked.在上面的示例中, DeviceViewCellContent ViewButton元素都选中了“启用用户交互”复选框。
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:如果自定义单元格实现为 UITableViewCell 的子类,并且自定义单元格视图是从另一个 xib 文件动态加载的,则您必须再做一件让大多数人感到困惑的事情,但我稍后会解释原因:
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.其中self是您创建的 UITableViewCell 的子类。 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.同样,如果自定义单元格是作为 UITableViewCell 的子类实现的,并且自定义单元格视图是从另一个 xib 文件动态加载的,则需要执行上述操作。 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. self.contentView与您在 Interface Builder 中看到的Content View ,它是由系统创建的default content view ,位于自定义单元格视图中的所有其他视图/元素的顶部,并且它掩盖了其余视图视图层次结构。 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.因此,在自定义单元格视图中的 UI 元素可以接收用户交互事件之前,您需要为其禁用用户交互。 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!欢迎讨论!

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 UITableViewController to a storyboard, an UITableViewCell is added to it's UITableView automatically.当您将UITableViewController添加到故事板时,会自动将 UITableViewCell 添加到它的UITableView If you take a look at this UITableView , you'll see a contentView in it.如果你看看这个UITableView ,你会在其中看到一个contentView

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 .因此,当您子类化UITableViewCell并使用 .xib 创建它时,您必须单击并拖动UITableViewCell到 xib 而不是默认的UIView Then don't forget to set the File Owner class and the UITableViewCell class to your subclass.然后不要忘记将 File Owner 类和UITableViewCell类设置为您的子类。

The solution to this problem is;这个问题的解决方法是;

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

This works for me.这对我有用。

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 之上,使它们无法选择。

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 (ie it was a subview of a subview).我的问题是我有一个 UIButton,它不是 cell.contentView 的直接子视图(即它是子视图的子视图)。 I had to add my button directly to cell.contentView in order for it to be tappable.我必须将我的按钮直接添加到 cell.contentView 以便它可以被点击。 (iOS v8+) (iOS v8+)

我的细胞轮廓

In my case, everything that I would like the user to interact with is outside of the ContentView.就我而言,我希望用户与之交互的所有内容都在 ContentView 之外。 Also I do not want the cell to be highlighted when it is selected.此外,我不希望单元格在被选中时突出显示。

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];

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.Frame to self.frame at the overwrite method - (void)layoutSubviews the problem will be solved有一个建议,您可以在覆盖方法self.frame self.contentView.Frame设置为self.frame - (void)layoutSubviews问题将得到解决

As others have pointed out, always add subviews to the cell's content view.正如其他人指出的那样,始终将子视图添加到单元格的内容视图中。 If you have overridden layoutSubviews method, make sure to call the super implementation.如果您覆盖了layoutSubviews方法,请确保调用超级实现。 Forgetting to call the super implementation yields unexpected results.忘记调用 super 实现会产生意想不到的结果。

I had same issue with a UIButton in a prototype cell in my storyboard.我的故事板中原型单元格中的 UIButton 有同样的问题。 I solved it by checking "User Interaction Enabled" on the prototype cell.我通过检查原型单元上的“启用用户交互”来解决它。 This worked on iOS 7.1.这适用于 iOS 7.1。

The most obvious mistake I haven't seen described here is that you MUST have a selectedBackgroundImage for a UIButton to look different when selected.我在这里没有看到的最明显的错误是你必须有一个 selectedBackgroundImage 来让 UIButton 在被选中时看起来不同。 These are not like, say, UITableViewCells.这些不像 UITableViewCells。

myButton.setBackgroundImage(UIImage(named: "ButtonBackgroundSelected.png"), forState: .Highlighted)

I had subclassed UITableViewCell and solved it by changing this code:我已经继承了 UITableViewCell 并通过更改此代码解决了它:

addSubview(myButton)

to this:对此:

contentView.addSubview(myButton)

in my UITableViewCell subclass.在我的 UITableViewCell 子类中。

I faced a similar issue on both UITableViewCell and UICollectionViewCell.我在 UITableViewCell 和 UICollectionViewCell 上都遇到了类似的问题。 Few steps to follow:要遵循的几个步骤:

  1. Always add subviews to content view of the cell view and NOT the cell view itself始终将子视图添加到单元格视图的内容视图而不是单元格视图本身
  2. If you are adding a container view (which contains your button), disable the auto layout of the container view and enable the auto layout only for the contents inside the container view.如果您要添加容器视图(其中包含您的按钮),请禁用容器视图的自动布局并仅对容器视图内的内容启用自动布局。 Use frame layout for the container view.为容器视图使用框架布局。

In short, TableView/CollectionView cells cannot take actionable subviews (UIButton, UISegmentedControl) that are auto-layout enabled.简而言之,TableView/CollectionView 单元格不能采用启用了自动布局的可操作子视图(UIButton、UISegmentedControl)。 If you have a need for it, either disable auto-layouts and provide frames (or) put them inside a container view (Container view should have auto layout disabled) and add the actionable views (with auto layout) as subviews to the container view.如果您需要它,请禁用自动布局并提供框架(或)将它们放在容器视图中(容器视图应禁用自动布局)并将可操作的视图(具有自动布局)作为子视图添加到容器视图.

In my case, I need to check the "User Interaction Enabled" for the cell itself (not just the contentView) as well.就我而言,我还需要检查单元格本身(不仅仅是 contentView)的“启用用户交互”。 But as noted in other answers, there could be multiple reasons for why this is happening so I am sure this won't work for every case.但正如其他答案中所指出的,发生这种情况的原因可能有多种,所以我确信这不适用于每种情况。

在此处输入图片说明

当您使用 Interface Builder (Storyboard) 时,请务必从Content View选中User Interaction Enabled复选框

None of these answers worked for me.这些答案都不适合我。

The only way I was able to fix this problem was to set the table cell selection from single to none.我能够解决这个问题的唯一方法是将表格单元格选择从单个设置为无。 After that, I was able to receive button call backs.在那之后,我能够收到按钮回调。

足够连线,对我来说实际的解决方案是转到故事板,选择单元格中的 contentview,然后检查“userInteractionEnabled”...否则即使您在按钮或单元格上启用了 userInteraction,contentview 也将始终阻止该操作.

我对那个实现了一些内部功能并处理自我的单元有透明的看法。

I checked everything but I made silly mistake of In storyboard my button's superview was below some other view.我检查了所有内容,但我犯了一个愚蠢的错误,在情节提要中,我的按钮的超级视图低于其他视图。 so touch was not passing to my button.所以触摸没有传递到我的按钮。

My problem was that I was adding UIButton as subview of UIImageView which has user interaction disabled by default.我的问题是我将UIButton添加为UIImageView子视图,默认情况下禁用了用户交互。 So I had to enable it:所以我必须启用它:

imageView.isUserInteractionEnabled = true

In Swift 5, I create a class UITableViewCell and I create a UIButton.在 Swift 5 中,我创建了一个 UITableViewCell 类并创建了一个 UIButton。

class DashboardingGameTableViewCell: UITableViewCell {
    
    // MARK: Setup Views
    
    var showInteractiveButton: UIButton = {
        let interactive = UIButton()
        interactive.setTitle("", for: .normal)
        interactive.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
        interactive.setImage(UIImage(systemName: "ellipsis"), for: .normal)
        interactive.tintColor = .white
        interactive.backgroundColor = .clear
        interactive.translatesAutoresizingMaskIntoConstraints = false
        
        return interactive
    }()
    ...

and on my在我的

class DashboardingGameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource...

The UIButton was not clickable UIButton 不可点击

for It works I have to make...因为它有效我必须做...

on DashboardingGameViewController: UITableViewCell...在 DashboardingGameViewController 上:UITableViewCell...

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.isUserInteractionEnabled = false
    addSubview(showInteractiveButton)
...

on DashboardingGameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource...在 DashboardingGameViewController 上:UIViewController、UITableViewDelegate、UITableViewDataSource...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier,
                                                            for: indexPath) as! DashboardingGameTableViewCell
        
        cell.showInteractiveButton.addTarget(self, action: #selector(interactiveGames(_:)), for: .touchUpInside) 
    ...
  
// MARK: - Private Funcs 
    
@objc private func interactiveGames(_ button: UIButton) {
                present(ShowInteractiveGameViewController(), animated: true, completion: nil)
}

If you have a CUSTOM cell class, simply include self.selectionStyle = .none in awakeFromNib()如果您有自定义单元格类,只需在awakeFromNib()包含self.selectionStyle = .none

override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.selectionStyle = .none
    }

Okay, I've found a solution to this issue, but it appears to be a huge hack;好的,我已经找到了解决此问题的方法,但这似乎是一个巨大的问题; however, it is all I was able to come up with (nothing else here worked for me).然而,这就是我所能想到的(这里没有其他东西对我有用)。

Overtop of my button I've added a UITextField which delegates to the table view cell in question.在我的button上方,我添加了一个 UITextField,它委托给有问题的表格视图单元格。 That tableViewCell instance implements -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField and returns NO .该 tableViewCell 实例实现-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField并返回NO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM