无法让didSelectRowAt为TableView工作

我遇到麻烦得到didSelectRowAt方法工作在一个常规的ViewController内的TableView 。 我已经确定表的delegatedata source是在ViewController代码中设置的。 这个ViewController用一个search查询的结果填充tableview单元格到一个API ,并且单元格数据的渲染工作正常。

这只是没有注册的didSelectRowAt方法。 我曾尝试在Main.storyboard上手动添加相同的代理信息,但小号+符号不会触发任何popup窗口。 我想知道是否有需要修复的Main.storyboard中的东西。 我还附加了ViewControllerTableView连接检查器的图像。 我是iOS开发新手,没有太多的移动devisegraphics界面的经验,所以我认为这是有些东西,但也许我错了。

这是我的代码的基本版本:

 class SearchViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! @IBOutlet var searchBar: UISearchBar! ...variable declarations .... override func viewDidLoad() { super.viewDidLoad() self.hideKeyboardWhenTappedAround() searchResults = [] searchBar.delegate = self tableView.dataSource = self tableView.delegate = self } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return searchResults!.count; } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "searchTableViewCell", for: indexPath) as! SearchTableViewCell if(searchActive && !(self.searchResults?.isEmpty)!) { (doing some stuff with search results here...works fine) } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("hello!") } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { print("search text \(searchText)") getSearchResultJSON(term: searchText) { json in let data = json as! [Dictionary<String, String>] self.searchResults = data } self.tableView.reloadData() } ... } 

[ Viewcontroller连接检查器 ]

[ TableView连接检查器 ]

编辑:作为一个健全性检查,如果searchasynchronousfunction正在改变任何东西,我只是试图删除所有search相关的代码,并从硬编码虚拟variables数组填充tableview。 它工作显示虚拟variables,但仍然没有能力select一个单元格,并得到任何反应。 我也看到一对夫妇提到,我以前有一个与didDeSelectRowAt而不是didSelectRow在一个错字,已被修复,但行为是相同的。

这最终与在我写的hideKeyboardWhenTappedAround()扩展中出现的轻击手势相关

find了! 罪魁祸首是self.hideKeyboardWhenTappedAround() ,这是我写的隐藏键盘的扩展。 这干扰了一个单元的水龙头,因为它确实利用了UITapGestureRecognizer 。 感谢大家的提示。

您正在使用didDeselectRowAt而不是didSelectRowAt

编辑

那么,请使用下面的代表然后

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } 

并使您的控制器符合UIGestureRecognizerDelegate

如果您在主视图上使用轻击手势,则表格视图单元格select方法无法正常工作。

在你上传的图片中,委托和数据源没有连接到ViewController。 删除viewdidload(tableview.delegate = self)中的代码,并将它们连接到故事板。