23

I know this issue is already been asked few times in SO. Despite trying those out, I am still unable to solve my problem.

I am using a UITableView inside a UIViewController. I have a custom UITableViewCell which has couple of buttons in it. However, I am not able to make the Button respond to Click event.

The development environment is iOS 9 and Swift 2

Snippets used :

BranchNearMeTableViewCell.swift contains

@IBOutlet weak var btnDetails: UIButton!

view controller class

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("branchNearTableCell") as! BranchNearMeTableViewCell

 cell.btnDetails.tag = indexPath.row
        cell.btnDetails.addTarget(self, action: "showDetails:", forControlEvents: .TouchUpInside)

}

func showDetails(sender: UIButton){

        print("Button Pressed:")
    }

Additional Info:

TableView and TableCellView has User interaction disabled in Interface builder since don't want the entire cell to be clickable.

UIButton inside TableViewCell has User Interaction enabled.

Being an iOS noob, I may be making a silly mistake which I might have overlooked.

Similar questions that I checked include:

SO1

SO2

SO3

I Deeply appreciate any help regarding this question.

Community
  • 1
  • 1
user2695433
  • 1,843
  • 4
  • 21
  • 44

12 Answers12

59

I faced a similar issue. I was programmatically adding an UIButton to the UITableViewCell via addSubview. The button would not respond to touch events. Using Debug View Hierarchy, I finally discovered that any subviews added to the UITableViewCell was behind contentView, which was blocking user input from reaching the UIButton. The issue was resolved by adding the UIButton to contentView instead of the UITableViewCell.

Sargis
  • 1,072
  • 1
  • 17
  • 29
Peter Liaw
  • 601
  • 1
  • 6
  • 4
  • Thanks a bunch! always used addSubview positioning stuff inside of my cells, but contentView.addSubview is what worked. +1 for linked tutorial as well – Tunscopi Nov 17 '20 at 08:42
  • This answer worked for me. Thank you soooo much – IsuNas Labs Dec 22 '20 at 06:42
  • This answer should have 100 upvotes more :D – hellofabrizio Feb 08 '21 at 16:02
  • 1
    adding your views to contentView has its own demerits. I'm a noob, found this helpful a week ago, now after reading a lot to fix my bug, had to downvote this answer. – Amit Khetan Mar 07 '21 at 09:04
  • 3
    This must have changed in later versions of iOS. In some old cold bases adding the button to the cell directly always worked. However at some point it stopped working and now I have to add the button the contentView – MobileMon Mar 29 '21 at 12:36
  • 1
    Thank you! I have been searching for hours – btraas Jun 30 '21 at 18:56
11

I would have userInteractionEnabled set to true on the table view cell as well. I would prevent taps using the UITableView allowsSelection to false

Also remember to remove the target and action in tableView:cellForRowAtIndexPath: since the cells are recycled, the button might already have the target and action, it might add a second.

Kyle Redfearn
  • 2,086
  • 16
  • 34
8

I found a simple solution:

Inherits UITableViewCell, and override init()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    //init subviews, eg. self.switch = UISwitch()
    super.init(style: style, reuseIdentifier: reuseIdentifier)
                
    // add this line magic code 
    contentView.isUserInteractionEnabled = true
                
    //add subviews, e.g. self.addSubView(self.switch)
}
luffy
  • 81
  • 1
  • 2
4

I came across this issue today, with a button inside a static UITableview cell, that was not responding to user events. I realised the 'Content View' of the cell also has a 'User Interaction Enabled' tick box. Make sure you select the 'Content View' inside the UITableview cell in your Document Outline menu, then tick the box for 'User Interaction Enabled' in the Attributes Inspector - see attached photo for reference. 'User Interaction Enabled' also needs to be checked for the cell for this to work. Hope this helps. XCcode screen shot

3

You only have to do (in ViewDidLoad):

mTableView.delaysContentTouches = false
Eray Alparslan
  • 638
  • 6
  • 12
3

For programmatically created views, the only thing to remember is to declare buttons using lazy var in UITableViewCell. And also add subviews to contentView instead of the cell itself For example:

class CounterCell: UITableViewCell {

    lazy var incrementButton: UIButton = {
        let button = UIButton()
        button.setTitle("+", for: .normal)
        button.addTarget(self, action: #selector(incrementAction), for: .touchUpInside)
        return button
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(incrementButton)
        // Your constrains here
    }

    @objc func incrementAction() {
    }

}

When using programmatically views, there's no need to add .userInteractionEnabled flags.

Then to take the action out of the cell, just add a delegate and assign it from the UITableViewDataSource.

Camilo Ortegón
  • 3,178
  • 2
  • 24
  • 32
1

Also, make sure you are adding target actions to your buttons outside their setup. So instead of

let button: UIButton = {
    //addTarget...
}()

you can have a function to set up your buttons after something happens:

func setButtonsUp() {
    // myButton.addTarget
}
Taiwosam
  • 409
  • 6
  • 13
0

Ad a first sight nothing seems to be wrong with your code.
So I suggest you to add a background color to the superview of the button, why? because if the button is outside the frame of its superview it will never receive touches.
If you see that the button is not inside the background color probably you have an issue positioning the item, check constraints or whatever you are using. Check also the frame of the button.
You can also do both by inspecting the view at runtime, here a tutorial.

Andrea
  • 25,454
  • 10
  • 82
  • 127
  • Thanks Andrea, tried your suggession by adding background color, the button is positioned within the superview color. – user2695433 Feb 12 '16 at 08:31
0

I dont know what wrong in the code but i can suggest which i personally use and it works for me

In BranchNearMeTableViewCell.swift

@IBOutlet var btnDetails: UIButton!
@IBAction func btnDetailsClick(sender: AnyObject) {
    tapButton?(self)
}
var tapButton: (UITableViewCell -> Void)?

In Table view controller

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("branchNearTableCell") as! BranchNearMeTableViewCell

 cell.tapButton = {(user) in
             //User will be tablecell here do whatever you want to do here
        }

}

So if you click on button in table cell this cell.tapButton will be called you can do whatever you want to do here

Ajay Beniwal
  • 18,447
  • 8
  • 74
  • 93
  • May I know which all properties you have enabled and disabled in Design like User Interaction and Selection for TableView and TableViewCell – user2695433 Feb 12 '16 at 09:10
  • user interaction is enabled by default for button and if button interaction is enabled it should work irrespective of tableviewproperties – Ajay Beniwal Feb 12 '16 at 09:14
  • share gist for xib and view controllers files then only we can debug the issue more – Ajay Beniwal Feb 12 '16 at 09:21
0

The only things we need to do is in cellForRowAt just put:

cell.selectionStyle = .none

in this way, UITableview will bypass the touch of selecting cells and allow buttons inside our cells to be clickable.

Sajjad Sarkoobi
  • 475
  • 2
  • 16
0
  • set cell and cell content view isUserInteractionEnabled = true
  • Add Tapgesture to the button
  • Add a closure to handle gesture action
0

Add target for that button.

button.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

Set tag of that button since you are using it.

button.tag = indexPath.row

Achieve this by subclassing UITableViewCell. button on that cell, connect it via outlet.

Make sure button.isUserInteractionEnabled = true

To get the tag in the connected function:

@objc func connected(sender: UIButton){
    let buttonTag = sender.tag
}
AzeTech
  • 426
  • 8
  • 15