ED.
ED.

Reputation: 231

UITableView didSelectRowAtIndexPath called twice

Under certain circumstances, UITableView didSelectRowAtIndexPath is being called twice causing the error Pushing the same view controller instance more than once is not supported.

Here's are the sequence of events:

TableView::didSelectRowAtIndexPath.  
TableView::viewWillDisappear.  
PushedViewController::viewWillAppear.  
TableView::didSelectRowAtIndexPath.  
Error: Pushing the same view controller instance more than once is not supported'  

The only thing worth noting is that the UITableView is loading images asynchronously, but that never calls didSelectRowAtIndexPath. Also, the PushedViewController is reused to avoid having to reload it each time a cell is selected in the UITableView.

Anyone have any idea what may be causing this? Thanks.

Upvotes: 18

Views: 7427

Answers (5)

J A S K I E R
J A S K I E R

Reputation: 2184

Swift 5

In my case also helped to update the cells instead of this code:

tableView.reloadRows(at: [indexPath], with: .automatic)

to use this:

tableView.reloadData()

Upvotes: 0

ali ozkara
ali ozkara

Reputation: 5616

if you already created Storyboard Segue don't call;

[self performSegueWithIdentifier:@"TYPE" sender:self];

in this method;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 1

Arun Reddy
Arun Reddy

Reputation: 3733

This occurs because of the segue you are using inside the didSelectRowAtIndexPath method. Ensure that this segue is created by ctrl dragging from the view button on top of the source view controller to destination view controller. The error occurs when you create the segue by ctrl dragging from the table view cell to destination view controller. You do not need to change any of you code. Just delete the segue and create it in the correct way. Hope this solves your problem, if I got the question right.

Upvotes: 0

DenNukem
DenNukem

Reputation: 8254

I'm seeing this problem too, probably one out of a 1000 users gets affected, or less. I сan clearly see two didSelectRowAtIndexPath registering 50 ms one after another. My guess is that it is a bug in iOS - no new taps should be directed to old view once new view-controller has been pushed. Alas, it is likely up to us to write code guarding against this. Here's what I'm thinking:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
        if (self.navigationController.topViewController != self)        
            return;

    ... do other stuff

    }

Upvotes: 32

Steven Kramer
Steven Kramer

Reputation: 8513

Disable user interaction after the first "didSelectRow". It's possible for multiple taps to "stack up" during the transition.

It usually takes someone with amazing dexterity in their fingers to get this behavior, but still.

Upvotes: 1

Related Questions