5

I have a button inside a tableview cell (as shown in the below image), when I click the button,

didSelectRowAt indexpath

is not being triggered, could some one please suggest how I could do this ?

Please note: I am performing a set of actions on click of the button, in addition I would also like

didselectRowAt indexPath

to be triggered.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 print("Table view cell has been clicked")   
}

enter image description here

SpaceX
  • 2,814
  • 2
  • 42
  • 68
  • did you assign theTableView delegate to the class you are putting the code in? You should be working with UITableViewDelegate – Taichi Kato Feb 27 '17 at 00:40
  • what action is the button performing? It could call the `didselectRowAt indexPath` directly (or to be more correct, call some other function that is called by `didselectRowAt indexPath` and by the button's own method). Take a look at: http://stackoverflow.com/questions/20117163/didselectrowatindexpath-is-not-invoked-to-uibutton-of-cellforrowatindex – Son of a Beach Feb 27 '17 at 00:56
  • Possible duplicate of [didSelectRowAtIndexPath is not invoked to UIButton of cellForRowAtIndex](http://stackoverflow.com/questions/20117163/didselectrowatindexpath-is-not-invoked-to-uibutton-of-cellforrowatindex) –  Feb 27 '17 at 01:23
  • http://stackoverflow.com/questions/27429652/detecting-uibutton-pressed-in-tableview-swift-best-practices Swift alternative if you don't know how to add an action to an UIButton. –  Feb 27 '17 at 01:24
  • [SEE THIS LINK for better under standing please go through that question in link](http://stackoverflow.com/a/42364716/6818278) – Anuraj Feb 27 '17 at 05:00
  • Try connecting the button action to your subclassed UITableViewCell, inside that action call self.setSelected... – Juan Boero Feb 27 '17 at 07:51

3 Answers3

3

When you have multiple views in the cell they may receive the touch event and not the "cell" so didSelectRowAt will not be triggered. In that case add view.isUserInteractionEnabled = false to each view, label or button.

Waylan Sands
  • 321
  • 3
  • 11
0

Put the outlet to the button inside a custom cell class. Then set tag and add selector for button in cellForItemAt. This example is for colletionView just change to suit tableView.

If you want the button in each cell this is how you have to do it. Adding a static button to a single cell won't call didSelectItemAt because your tapping a button that doesn't have reference to a reusable cells indexPath.

This way we send the button.tag tot he function so we know which cell the button is related to.

class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    .... // Stuff

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    ....// Create Cell

    cell.deleteCellButton.tag = indexPath.item
    cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside)
    return cell
}

func deleteCellButtonTapped(_ sender: Any) {

     ... // Stuff

      print("Selector called")
}
}
WanderingScouse
  • 281
  • 1
  • 3
  • 16
  • Do not use the index path as a tag. It fails under many situations. – rmaddy Feb 27 '17 at 01:09
  • I read apple doesn't want you using it anymore. Whats the solution then? as this seems to be the way most people construct nested table views / collection views? – WanderingScouse Feb 27 '17 at 01:12
  • 1
    There are plenty of questions covering this so I won't provide too many details. But basically, if you need to determine the index path of a button in the button's action method, you calculate it based on the button's frame relative to the table view using the methods of the `UITableView` class. – rmaddy Feb 27 '17 at 01:14
  • @WanderingScouse, @rmaddy, I understand that you can attach a function to a button as shown above, but how do you activate the delegate methods like `didSelectRowAt indexPath` – SpaceX Feb 27 '17 at 01:23
  • @user44776 You don't. Simply have the button handler and the delegate method both call a common method. – rmaddy Feb 27 '17 at 01:24
  • @rmaddy, As I want to increase the cell height on button click, could I replicate `heightForRowAt IndexPath` inside IBAction of button ? – SpaceX Feb 27 '17 at 01:31
0

If you are adding button or gesture on UITableViewCell , didselectRowAt indexPath not invoked . You can add button on remaining UI UITableViewCell than clear color apply on button . User didn't show button and you can perform didselectRowAt indexPath method task . If you wanna indexpath in button method code give below.

 func btnChildDropDown(sender:UIButton)
    {
       let cell = sender.superview?.superview as! ChildAgeTableViewCell
       let indexPath = self.tblViewAddRooms.indexPath(for: cell)

    }
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35