[SOLVED] UIButton not responding used in a custom UITableViewCell

Issue

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.

Solution

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.

Answered By – Kyle Redfearn

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *