-1

I have a button in a custom cell

class CardTableViewCell: UITableViewCell {
    @IBOutlet weak var detailsButton: UIButton!
}

in my view controller then I do this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //.....more code....
    openDealCell.detailsButton.addTarget(self, action: "push:", forControlEvents: UIControlEvents.TouchUpInside)      
}

and then:

func push(sender:AnyObject) {
    println("tap")
}

But when I clicked the button nothing happens.

What I am doing wrong?

Azat
  • 6,745
  • 5
  • 31
  • 48
Mat
  • 6,236
  • 9
  • 42
  • 55
  • check detailsButton may not be linked to UITableViewCell in your interface builder. print the detailsButton in cellForRowAtIndexPath delegate method and check wether object exits or not? – Shashi3456643 Apr 28 '15 at 16:50
  • @Shashi3456643 when I print the button I get `> > ` – Mat Apr 28 '15 at 16:52
  • Yea code looks fine. Can you try linking func push(sender:AnyObject) method in interface builder and remove addTarget from cellForRowAtIndexPath – Shashi3456643 Apr 28 '15 at 16:56
  • @Shashi3456643 is it possible that the reason why is not woking is that the button is behind other views? – Mat Apr 28 '15 at 16:57
  • No Your code looks fine you have done right way. I was just telling you the other way to make this working. – Shashi3456643 Apr 28 '15 at 16:59
  • Either the outlet is not connected, the execution never goes to the addTarget line or openDealCell is not the recycled object you return in cellForRowAtIndexPath. – Tapani Apr 28 '15 at 17:10

2 Answers2

1

Fix it! The problem was not in the code but in my cell in the storyboard:

enter image description here

where user interaction for the cell wasn't enabled and all the other view didn't allow the user interaction.

enter image description here

Mat
  • 6,236
  • 9
  • 42
  • 55
0

Right now I created demo project with exactly the same code as your and it works. Please check here at GitHub. The issue definitely in the other stuff that you didn't show us, that code does not have any problems

Azat
  • 6,745
  • 5
  • 31
  • 48