Button in UITableViewCell not responding under ios 7

IosUitableviewUibuttonIos7

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Solution 3 - Ios

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:
enter image description here

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!

Solution 4 - Ios

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.

Solution 5 - Ios

The solution to this problem is;

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

This works for me.

Solution 6 - Ios

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.

Solution 7 - Ios

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

Solution 8 - Ios

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.

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

In my tableViewController method tableView:cellForRowAtIndexPath: I have the following code: 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];

Solution 9 - Ios

I have been get the same problem. The reason is

self.contentView.Frame.size.height

is always 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

Solution 10 - Ios

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. Forgetting to call the super implementation yields unexpected results.

Solution 11 - Ios

I had same issue with a UIButton in a prototype cell in my storyboard. I solved it by checking "User Interaction Enabled" on the prototype cell. This worked on iOS 7.1.

Solution 12 - Ios

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. These are not like, say, UITableViewCells.

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

Solution 13 - Ios

I had subclassed UITableViewCell and solved it by changing this code:

addSubview(myButton)

to this:

contentView.addSubview(myButton)

in my UITableViewCell subclass.

Solution 14 - Ios

I faced a similar issue on both UITableViewCell and 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. 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.

Solution 15 - Ios

In my case, I need to check the "User Interaction Enabled" for the cell itself (not just the contentView) as well. 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.

enter image description here

Solution 16 - Ios

When you are using Interface Builder (Storyboard) then be sure to check the User Interaction Enabled checkbox from the Content View

Solution 17 - Ios

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.

Solution 18 - Ios

Wired enough, the actually solution for me is to go to storyboard, select the contentview in the cell, and check the "userInteractionEnabled"... otherwise even if you have enabled userInteraction at the button or the cell, contentview will always block the action.

Solution 19 - Ios

I had transparent view on that cell which implemented some internal features and handled on touches on self.

Solution 20 - Ios

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.

Solution 21 - Ios

My problem was that I was adding UIButton as subview of UIImageView which has user interaction disabled by default. So I had to enable it:

imageView.isUserInteractionEnabled = true

Solution 22 - Ios

In Swift 5, I create a class UITableViewCell and I create a 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

for It works I have to make...

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

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

Solution 23 - Ios

If you have a CUSTOM cell class, simply include self.selectionStyle = .none in awakeFromNib()

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

Solution 24 - Ios

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. That tableViewCell instance implements -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField and returns NO.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXavierView Question on Stackoverflow
Solution 1 - Iosb005t3rView Answer on Stackoverflow
Solution 2 - IosyonixView Answer on Stackoverflow
Solution 3 - Iosus_davidView Answer on Stackoverflow
Solution 4 - IosdulganView Answer on Stackoverflow
Solution 5 - IosMuhammad Arqam OwaisView Answer on Stackoverflow
Solution 6 - IosCollinView Answer on Stackoverflow
Solution 7 - IosmikejonesguyView Answer on Stackoverflow
Solution 8 - IosAdrian SilveanuView Answer on Stackoverflow
Solution 9 - IosSnailView Answer on Stackoverflow
Solution 10 - IosdiatcheView Answer on Stackoverflow
Solution 11 - IosMike TaverneView Answer on Stackoverflow
Solution 12 - IosAppreciateItView Answer on Stackoverflow
Solution 13 - IosBavafaaliView Answer on Stackoverflow
Solution 14 - IosVisuView Answer on Stackoverflow
Solution 15 - IosJove KuangView Answer on Stackoverflow
Solution 16 - IosgpichlerView Answer on Stackoverflow
Solution 17 - IosIsaac PaulView Answer on Stackoverflow
Solution 18 - IosSummerView Answer on Stackoverflow
Solution 19 - IosNikolay ShubenkovView Answer on Stackoverflow
Solution 20 - IosSiddhesh MahadeshwarView Answer on Stackoverflow
Solution 21 - IosRobert DreslerView Answer on Stackoverflow
Solution 22 - IosBraveView Answer on Stackoverflow
Solution 23 - IosRuchir BaroniaView Answer on Stackoverflow
Solution 24 - IosdaveView Answer on Stackoverflow