user3858610
user3858610

Reputation: 175

UITableViewCell didSelectRowAtIndexPath is very slow at responding to selection

All I want is for a quick tap of the cell to launch a new view. The launching works but it takes a very long time (3-5 seconds) of holding on the cell to get it to register that it has been selected. Is this normal behavior? I can't find any way to respond to a simple .touchUpInside event. So what am I doing wrong? I have poured through the Apple documentation for the UITableViewCell and cannot find any fields or settings that would help me here. Thanks in advance!

EDIT: in the view controller viewDidLoad:

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

In the 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)
}

EDIT # 2:

I also want to make the point that I don't have any GestureRecognizers in my code at all.

Upvotes: 1

Views: 1627

Answers (2)

Jeremy Andrews
Jeremy Andrews

Reputation: 837

I had a similar problem. I had set-up a long press recognizer on a cell, like this:

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

I then wanted to tap on a cell in another tableview within the same view. No matter what I did - even removing the above code - the only way I could get :

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

to respond to a tap was to drag a tap recognizer onto the table cell. Now works perfectly. I think its possibly a bug which causes the long press to get stuck.

Upvotes: 0

user3858610
user3858610

Reputation: 175

Figured it out. Although I had not directly added any Gesture Recognizers to that file, I had added them indirectly by using a function I had made that allowed me to tap out of keyboards to make them disappear. All I had to do was to add this one line of code to that function and it works perfectly now. Thank you @mat for the help!

Here is the culprit

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

Upvotes: 2

Related Questions