DidSelectRowAtIndexPath not called (Swift)

I have been in circles for a while, and nothing I found in related posts seems to solve the problem. I programmatically add a table to a custom UIView. The text of the table and row is displayed correctly, but none of them called SelectRowAtIndexPath and does not light up during the launch of this simulator when trying to click on any of the rows.

The relevant bits of my code are 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) } } 
+6
source share
7 answers

Removing TapGestureRecognizer worked for me!

  // var backgoroundTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard") // self.view.addGestureRecognizer(backgoroundTap) 
+19
source

The reason for this may be that you are using a panorama gesture in the view. for example, AAA , the mentioned removal of your gestures for panorama will do the trick. But if you still want to use the panorama gesture, you can do the following

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

Creating cancelsTouchesInView, false will enable all of your clicks.

+7
source

Remove exclamation marks:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } 
+3
source

I have been having the same problem for a long time, I think that this is a bug in the UITableView programmatically when creating the UIView class.

My temporary solution, and although this is not a good idea, but it works. It is necessary to place the button with the size of the cell and perform the action sent by the protocol in order to achieve at least a simulation of this action.

This is not a good practice, but she could not stay there longer.

0
source

This was deprecated in Swift 3:

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

And is replaced by:

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

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

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

Take a close look to make sure you are using this feature. The compiler will not complain if you have NSIndexPath or you don’t have _ in front of the tableView, but it will not work. Therefore, use this function, set a breakpoint, and you will see that it goes to this point when you click on the line.

I tested this with a static table (all cells of static content).

0
source

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

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

I named the prototype identifier in the Attributes Inspector.

It worked for me. Apparently, I did not have any code to change.

0
source

Source: https://habr.com/ru/post/975466/


All Articles