0

I am trying to do what I believed was a simple task using Swift. I have created a Custom TableView Cell and added a button to it. I want to be able to just see when you click on the button some action take place. Unfortunately, when I click on the button there is nothing happening. Can someone shed some light on this? I have placed the code below:

Custom TableView Cell:

import UIKit

class TestTableViewCell: UITableViewCell {

@IBOutlet weak var testBtn: UIButton!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
func buttonTappedOnCell()
{
    println("buttonTapped!")
}

}

Below is the code in the View Controller which the call is being made:

  func tableView(tableView: UITableView,
    cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {
        tableView.registerNib(UINib(nibName:"TestTableViewCell", bundle:nil), forCellReuseIdentifier:"TestButtonCell")
 let cell =
        tableView.dequeueReusableCellWithIdentifier("TestButtonCell", forIndexPath: indexPath) as TestTableViewCell

           cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)
        return cell
}

I also tried changing the call to include the ':' -> cell.testBtn.addTarget(cell, action: "buttonTappedOnCell:", forControlEvents: UIControlEvents.TouchDown) but this did not work. Another thing I tried is moving the call function into the view controller and having the target action point to self. This unfortunately did not work either. A quick example of this working would be great. At the end of day would like to be able to have a custom table view cell that contains 6 radio buttons allowing the user to select just one. But baby steps first at this point. Thanks for any help.

jscs
  • 63,694
  • 13
  • 151
  • 195
doubleace3
  • 101
  • 1
  • 13
  • At a minimum, I'd move the `.addTarget(...` into the cell subclass. You don't need to re-assign the button's target each time you dequeue it. So, instead, in `awakeFromNib`, do something like: `testBtn.addTarget(self, action: "buttonTappedOnCell", forControlEvents:.TouchUpInside)` – mbm29414 Feb 12 '15 at 20:23
  • In addition, `registerNib` really only needs to be once per `UITableViewController` lifetime, move it into `viewDidLoad` – David Berry Feb 12 '15 at 20:29
  • Just checking the basics: are you sure you have set up your `IBOutlet` to the cell's button correctly? If it's `nil`, it could be the reason it's not forwarding its touch event. – mbm29414 Feb 12 '15 at 21:02

1 Answers1

0

I think the issue is caused by the event you specified in the code.

 cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)

Change that ToouchDown to TouchUpInside

cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchUpInside)

Also why are you setting it programmaticaly ? From your code I think you have the button on your storyboard. So why should you add the IBAction programmaticaly ? (Since the action is also invoked on the same class).

Declare your buttonTappedOnCell as an IBAction and connect it directly to your button on storyboard/xib.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I agree with changing to this event, but why do you think that would have caused the action method not to fire? – mbm29414 Feb 12 '15 at 20:21
  • @mbm29414 There could be several reasons. 1) Due to the wrong event subscribed (as in my answer) 2) User Interaction may be false etc – Midhun MP Feb 12 '15 at 20:23
  • Yes, but `TouchDown` will **ALWAYS** fire before `TouchUpInside` (i.e., you can't get a `TouchUpInside` without first getting a `TouchDown`), whereas `TouchUpInside` may not fire based on user interaction. I agree with **using** `TouchUpInside`, but I can't see how it will fix the problem the OP is having. – mbm29414 Feb 12 '15 at 20:24
  • I actually had the TouchUpInside even previously but changed it to use TouchDown for testing. I changed back but still have the same issue. – doubleace3 Feb 12 '15 at 20:26
  • I have UserInteraction checked in IB for the button and the cell as well. – doubleace3 Feb 12 '15 at 20:27
  • @doubleace3: is your button enabled and user interaction is set to true ? Also why are you setting it programmaticaly ? From your code I think you have the button on your storyboard. So why should you add the IBAction programmaticaly ? (Since the action is also invoked on the same class) – Midhun MP Feb 12 '15 at 20:28
  • As an even better solution, set up the target action in the storyboard itself. Control-drag from the button to the cell and then select your function. You'll probably need to add the `@IBAction` tag to your function declaration first. – David Berry Feb 12 '15 at 20:31
  • @doubleace3: if you have the issue still, can you post an image of your cell design ? – Midhun MP Feb 12 '15 at 20:40
  • Yes button is enabled and user interaction was set to true. I had tried the method you asked previously as well but to no success. I must be missing something somewhere. Below is a snapshot of the tableview cell in IB: – doubleace3 Feb 12 '15 at 20:58
  • @doubleace3: I didn't see any link to your snapshot – Midhun MP Feb 12 '15 at 21:04
  • I can't add the image right now but I have attached the code with the action code into my tableview Cell: – doubleace3 Feb 12 '15 at 21:21
  • `import UIKit class TestTableViewCell: UITableViewCell { IBOutlet weak var testBtn: UIButton! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } // func buttonTappedOnCell() // { // println("buttonTapped!") // } IBAction func buttonTapped(sender: AnyObject) { println("button clicked!") } }` – doubleace3 Feb 12 '15 at 21:25
  • here is the link to the image of the tableCell in IB: https://letscrate.com/f/doubleace3/misc/tableViewCell.png – doubleace3 Feb 12 '15 at 21:49
  • @doubleace3: Your design seems fine, so the connected IBAction is not also working ? – Midhun MP Feb 12 '15 at 21:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70820/discussion-between-doubleace3-and-midhun-mp). – doubleace3 Feb 12 '15 at 21:54