Lucas Crostarosa
Lucas Crostarosa

Reputation: 1192

Recursive UITableViewCell button Action

I'm able to generate a UITableView and a UITableViewCell however my action is not working properly. My use case is similiar to this video

I was using this question as a reference and was uncesscessful

What am I missing? Something swift 3 related?

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var categories = ["foo", "bar"]

    func logAction(sender: UIButton!){
        print("HIT BUTTON YO")
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.categories.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {      
        let cell:CustomCell = tableView.dequeueReusableCell(withIdentifier: "categories", for: indexPath) as! CustomCell
        cell.label.text = categories[indexPath.row]
        let button = cell.button
        button?.tag = indexPath.row
        button?.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)
        return(cell)
    }
}


class CustomCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        //Configure view for selected state
    } 
}

Upvotes: 1

Views: 391

Answers (3)

Lucas Crostarosa
Lucas Crostarosa

Reputation: 1192

yea so I just deleted the view controller and remade and it works now ¯_(ツ)_/¯

Upvotes: 0

naturaln0va
naturaln0va

Reputation: 763

Try accessing the cell's button directly. Instead of

let button = cell.button
button?.tag = indexPath.row
button?.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)

Set the properties on the button directly.

cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)

Upvotes: 0

vadian
vadian

Reputation: 285270

There is a smarter way to handle a button action in a custom cell.

In the custom cell create a callback closure with no parameter / no return value and an IBAction connected to the button. In the action call the callback

class CustomCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!

    var callback : (()->())?

    @IBAction func logAction(_ sender : UIButton) {
        callback?()
    }
}

In cellForRow assign a closure to the callback, the index path is captured.

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categories", for: indexPath) as! CustomCell
    cell.label.text = categories[indexPath.row]
    cell.callback = {
        print("Button pressed", indexPath)
    }
    return cell
}

Note: return is not a function (no parentheses).

Upvotes: 2

Related Questions