12

I've been going in circles for a while and nothing I've found in related posts seems to solve it. I'm programmatically adding a table to a custom UIView. The table and row text displays correctly, but neither didSelectRowAtIndexPath nor willdSelectRowAtIndexPath fire when I run this on the simulator and try to click on any of the rows.

The relevant bits of my code below:

import UIKit
import AVFoundation

@IBDesignable
class PerformanceQuestionView: UIView, UITableViewDelegate, UITableViewDataSource {

var optionsTable = UITableView(frame: CGRectMake(10,200,250,200))

var optionItems = ["One", "Two", "Three", "Four"]

convenience init(rect: CGRect, performanceQuestion:PerformanceQuestion?) {
    self.init(frame: rect)

    NSLog("PerformanceQuestionView.init()")

    self.optionsTable.dataSource = self
    self.optionsTable.delegate   = self
    self.optionsTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.optionsTable.allowsSelection   = true
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    NSLog("numberOfRowsInSection")
    return optionItems.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    NSLog("cellForRowAtIndexPath")
    var cell:UITableViewCell = self.optionsTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel.text = self.optionItems[indexPath.row]

    return cell
}

func tableView(tableView: UITableView!, willSelectRowAtIndexPath indexPath: NSIndexPath!) -> NSIndexPath! {
    NSLog("You will select cell #\(indexPath.row)!")
    return indexPath
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    NSLog("You selected cell #\(indexPath.row)!")
}

override func layoutSubviews() {

    super.layoutSubviews()

    self.addSubview(optionsTable)
}

}
4
  • do you connect your Tableview data-source and delegate from storyboards.? Sep 18, 2014 at 5:20
  • @NitinGohel They're connected in the init method. Sep 18, 2014 at 5:37
  • bettero to check once connect from storyBoard Sep 18, 2014 at 5:45
  • 2
    How are you instantiating the PerformanceQuestionView? What are you passing in for the rect? When UI elements are not responding to touches, it's usually because they are falling outside the bounds of their superview.
    – rdelmar
    Sep 18, 2014 at 5:53

8 Answers 8

27

Removing the TapGestureRecognizer worked for me!!!

    // var backgoroundTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
    // self.view.addGestureRecognizer(backgoroundTap)
3
  • 1
    works for me as well but what if we need to use the tap gesture...? Jan 22, 2016 at 6:19
  • 2
    create a separate view and load the labels and buttons inside that view. Then add the gesture for that view alone. It will work.
    – AAA
    Jan 22, 2016 at 6:34
  • Thank will try it..! Jan 23, 2016 at 9:35
22

Reason for this could be, you are using a pan gesture in the view. like AAA mentioned removing your pan gesture will do the trick. But if you still want to use the pan gesture you could do the following

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
    tap.cancelsTouchesInView = false

Making cancelsTouchesInView, false will enable all you taps.

2
  • Thank you!! I thought that I will never find the reason why my didSelectedRow method was not called
    – origds
    Aug 13, 2018 at 8:52
  • WHERE do I put this?? WHICH variables do I need to change to make it work?? GAH! It's telling me view is undeclared
    – velkoon
    Dec 7, 2018 at 1:59
3

Remove the exclamation points:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}
3
  • 3
    @JavierArtiles Something in your view hierarchy might be blocking the touches. Is the PerformanceQuestionView's frame at least as big as the table's frame? Sep 18, 2014 at 5:38
  • 1
    @Aaron Brager thanks, i have the same error I remove the UITapGestureRecognizer and works fine !!
    – Fabio
    Feb 27, 2015 at 21:44
  • Does not work for me too, maybe protocols are missing, but I'm not a pro
    – Nicholas
    Apr 25, 2015 at 14:11
2

You need to add below code for Tap or Pan gesture.You need to tap.cancelsTouchesInView = false

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
    tap.cancelsTouchesInView = false

OR

     let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.mainBlurViewAction))
      mainBlurView.addGestureRecognizer(gesture)
      gesture.cancelsTouchesInView = false

Then it will work

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   //Call According requirement
}
1

I have the same problem long time, I am for thinking it's a bug in UITableView programmatically when creating a UIView class.

My temporary solution and although it is not a good idea, but it works. It is to place a button the size of the cell and have an action dispatched by a protocol, to achieve at least simulate this action.

It is not good practice but could not stay stuck there longer.

1

For Swift 3.0, you need to use this function exactly:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 //Input your code here

}

Look very closely to make sure you are using this function. The compiler won't complain if you have NSIndexPath or you don't have _ before tableView but it won't work. So use this function, put a breakpoint in and you will see it go to that point when you tap on a row.

I was testing this with a static table (all static content cells).

0

This was deprecated in Swift 3:

override func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
    ... 
}

And replaced with:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    ...
}
0

I tried all of the methods listed in this question and others. I finally deleted the table from my storyboard and started again.

In my storyboard, I added back the table, added a prototype cell. I connected the ViewController to the table. I connected the table to my dataSource and delegate methods.

I named the prototype identifier in the Attributes Inspector.

This worked for me. I did not apparently have any code to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.