Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 273
ED. Avatar asked Apr 16 '11 16:04

ED.


3 Answers

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

    }
like image 65
DenNukem Avatar answered Nov 01 '22 05:11

DenNukem


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.

like image 26
Steven Kramer Avatar answered Nov 01 '22 03:11

Steven Kramer


if you already created Storyboard Segue don't call;

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

in this method;

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

like image 1
aliozkara Avatar answered Nov 01 '22 04:11

aliozkara