UITableViewCell didSelectRowAtIndexPath响应选择非常慢

时间:2017-01-13 21:19:17

标签: ios swift uitableview

我想要的只是快速点击单元格以启动新视图。启动工作但是需要很长时间(3-5秒)保持单元格以使其注册它已被选中。这是正常的行为吗?我找不到任何方法来回应简单的.touchUpInside事件。那么我做错了什么?我已经通过了UITableViewCell的Apple文档,找不到任何可以帮助我的字段或设置。提前谢谢!

编辑: 在视图控制器viewDidLoad中:

self.searchTable.dataSource = self
self.searchTable.delegate = self
self.searchTable.register(UINib(nibName: "ItemCellView", bundle: nil), forCellReuseIdentifier: "itemCell")
self.searchTable.reloadData()

在viewController中:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
    searchItems.removeAll()
    self.searchBar.endEditing(true)
    requests.requestSearch(query: searchBar.text!, filter: "RATING", vc: self)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (self.searchItems.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.searchTable.dequeueReusableCell(withIdentifier: "itemCell")! as! ItemCell
    cell.formatCell(item: self.searchItems[indexPath.row])

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let item = self.searchItems[indexPath.row]
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "item") as? ItemController
    vc?.item = item
    self.navigationController?.pushViewController(vc!, animated: false)
}

编辑#2:

我还想说明我的代码中根本没有任何GestureRecognizers。

2 个答案:

答案 0 :(得分:1)

想出来。虽然我没有直接在该文件中添加任何手势识别器,但是我通过使用我制作的功能间接添加了它,这使我能够从键盘中取出以使它们消失。我所要做的就是将这一行代码添加到该函数中,它现在可以完美地运行。谢谢@mat的帮助!

这是罪魁祸首

extension UIViewController {
func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
    view.endEditing(true)
}

答案 1 :(得分:0)

我有类似的问题。 我已经在一个单元格上设置了一个长按识别器,如下所示:

let press = UILongPressGestureRecognizer(target: self, action: #selector(myMethodToHandlePress(gesture:)))
    press.minimumPressDuration = 0.2
    press.cancelsTouchesInView = false
    ToDoView.addGestureRecognizer(press)

然后,我想在同一视图内的另一个表格视图中点击一个单元格。无论我做了什么-甚至删除了上面的代码-我都可以得到的唯一方法:

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

响应轻击是将轻击识别器拖动到表格单元格上。 现在可以完美运行了。 我认为这可能是导致长按卡住的错误。

相关问题