Sebastian Considine
Sebastian Considine

Reputation: 29

UIButton in table cell "unrecognized selector sent to instance"

I have a button in a table cell, when pressed it crashes the app with the error:

unrecognized selector sent to instance 0x7f9a39840a00 2016-11-25 15:32:04.310 App Name[19161:1264937] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App_Name.routineCell forwardPress:]: unrecognized selector sent to instance 0x7f9a39840a00'

Here's the code:

   internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return routineGroups.count
}

func cellButtonPress() {
    print("works")
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell:routineCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! routineCell

    cell.textLabel?.text = routineGroups[indexPath.row]

    cell.forwardButton.tag = indexPath.row
    cell.forwardButton.addTarget(self, action: #selector(routinesGroups.cellButtonPress), for: UIControlEvents.touchUpInside)

    return cell
}

I looked at the solutions here: Link1 and here Link2 but I get the same error every time. The cell has its own .swift file where it was dragged as an outlet: Cell.swift file

When the crash happens Xcode takes me to the AppDelegate.swift and shows this: crash goto

Does anyone know how to fix this?

Upvotes: 0

Views: 1444

Answers (2)

PGDev
PGDev

Reputation: 24341

For UIButton action in a UITableViewCell, you can use:

Method-1 - Handle the button tap even in the CustomTableViewCell itself using closure.

CustomTableViewCell class:

class CustomTableViewCell: UITableViewCell
{
    @IBOutlet weak var forwardButton: UIButton!
    var completionHandler : (()->())?

    @IBAction func cellButtonPressed(_ sender: UIButton)
    {
        if let handler = self.completionHandler
        {
            handler()
        }
    }
}

UITableViewDataSource method:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        cell.completionHandler = {[weak self] in self?.cellButtonPress()}
        return cell
    }

Method-2 - Handle the button tap event at the controller level.

UITableViewDataSource method:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        cell.forwardButton.addTarget(self, action: #selector(cellButtonPress), for: .touchUpInside)
        return cell
    }

Upvotes: -1

Jason Baracassa
Jason Baracassa

Reputation: 331

Seems like the problem is about forwardPress, not forwardButton nor cellButtonPress. Did you check the Outlet Inspector in the Interface Builder?

On some interface element (maybe the cell when reading the debugger), you may have an outlet not linked in code called forwardPress. You perform the action on the element, IB looks for the forwardPress method, which does not exist => crash.

Upvotes: 2

Related Questions