无法让 didSelectRowAt 为 TableView 工作 [英] Can't get didSelectRowAt to work for TableView

查看:26
本文介绍了无法让 didSelectRowAt 为 TableView 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 didSelectRowAt 方法用于常规 ViewController 内的 TableView.我已经确保在 ViewController 代码中设置了表的 delegatedata source.这个 ViewController 使用从搜索查询到 API 的结果填充 tableview 单元格,并且单元格数据的呈现工作正常.

I am having trouble getting the didSelectRowAt method to work for a TableView inside of a regular ViewController. I have already made sure that the delegate and data source for the table are set in the ViewController code. This ViewController populates the tableview cells with results from a search query to an API, and the rendering of cell data is working fine.

这只是没有注册的 didSelectRowAt 方法.我确实尝试在 Main.storyboard 上手动添加相同的委托信息,但是小 + 符号不会触发任何弹出窗口.我想知道 Main.storyboard 中是否有需要修复的东西.我还附上了 ViewControllerTableView 连接检查器的图像.我是 iOS 开发的新手,对移动设计的图形界面没有太多经验,所以我假设它在那里,但也许我错了.

It's just the didSelectRowAt method that is not registering. I did try manually adding the same delegate information on the Main.storyboard, but the little + sign won't trigger any popup windows. I am wondering if there is something in the Main.storyboard that needs fixing. I have attached the images of the ViewController and TableView connections inspector as well. I am new to iOS development and don't have much experience with graphic interfaces for mobile design, so I am assuming it's something there but maybe I am wrong.

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

Here's the basic version of my code:

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()
    }
...
}

[]

[]

作为对搜索异步函数是否正在更改任何内容的健全性检查,我只是尝试删除所有与搜索相关的代码并从硬编码的虚拟变量数组中填充 tableview.它可以显示虚拟变量,但仍然无法选择单元格并获得任何反应.我还看到一些提到我之前在 didDeSelectRowAt 而不是 didSelectRow at 上有一个错字,这已经被修复,但行为是一样的.

as a sanity check for if the search asynchronous function was changing anything, I just tried removing all search-related code and filling the tableview from a hardcoded dummy variable array. It worked to display the dummy variables, but still no ability to select a cell and get any reaction. I also saw a couple mentions that I had previously had a typo with didDeSelectRowAt instead of didSelectRow at, that has been fixed but the behaviour is the same.

这最终与我编写的 hideKeyboardWhenTappedAround() 扩展中发生的点击手势有关

This ended up being related to a tap gesture that occurs in the hideKeyboardWhenTappedAround() extension that I wrote

推荐答案

找到了!罪魁祸首是 self.hideKeyboardWhenTappedAround(),这是我为隐藏键盘而编写的扩展程序.这会干扰单元格的点击,因为它确实使用了 UITapGestureRecognizer.谢谢大家的提示.

Found it! The culprit was the self.hideKeyboardWhenTappedAround(), which is an extension I wrote to hide the keyboard. This interfered with the tap of a cell because it did indeed utilize UITapGestureRecognizer. Thanks for the hints everyone.

这篇关于无法让 didSelectRowAt 为 TableView 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆