1

I've got a UITableViewCell that contains a button, I handle this button click event follow this link Get button click inside UI table view cell,

Here is my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            cell.directCommentButton.tag = indexPath.row;

    cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
    return cell
}

func directCommentButtonClicked(sender: AnyObject) {
    // directComment = 1
    println("DIRECT COMMENT")
}

but there is an error directCommentButtonClicked]: unrecognized selector sent to instance 0x7f9e50590f10' and app has crashed. When I remove the sender: AnyObject, the error disappear, but I want to get the sender, how can I do that. Tks in advance

Community
  • 1
  • 1
maphongba008
  • 2,114
  • 4
  • 20
  • 33

1 Answers1

3

Just pass your action selector this way:

cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

you have to add : because your function accepts an argument.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165