UIButton not responding used in a custom UITableViewCell

17,047

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

You only have to do (in ViewDidLoad):

mTableView.delaysContentTouches = false

Solution 5

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

Share:
17,047
user2695433
Author by

user2695433

Updated on June 06, 2022

Comments

  • user2695433
    user2695433 almost 2 years

    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.