Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to dequeue multiple cells for the same index path, which is not allowed

I have a very simple UITableView that has worked just fine since iOS6....I have recently tried to make a new build going from iOS10-iOS11 and now I am getting this new NSInternalConsistency Exception that I have never seen before, it literally came out of left field with the new iOS build....

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: WorkOrderCell, index path: {length = 2, path = 0 - 0}'

I have reviewed numerous tutorials and I feel like there is nothing exceptionally wrong with this code..in fact normal grid load works fine, here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"WorkOrderCell" forIndexPath:indexPath];



    // Configure the cell...  do some work, return it



    return cell;
}

Has anyone else experienced this issue and what have you done to resolve it?

I should be a bit more specific, the normal UITableView does load fine, this specific issues is that I have a SearchBar, and when the user types into the search, I am filtering the results and displaying a results grid:

//Start Search/Filter Code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Job contains[cd] %@ or Address contains[cd] %@ or WorkOrderId contains[cd] %@ or Supervisor contains[cd] %@", searchText, searchText, searchText, searchText];
    self.filteredWorkOrders = [self.workOrders filteredArrayUsingPredicate:resultPredicate];
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

The filter code itself works fine, it's just that it triggers the UITableView delegates to fire, which starts dequeuing cells (which should be empty right in the search results grid??)

In any event I could use some help with this one please.....

like image 306
Brandon Slezak Avatar asked Dec 09 '17 02:12

Brandon Slezak


4 Answers

As the warning message said, just use

dequeueReusableCell(withIdentifier: "YourIdentifierCellHere")

Without

for: indexPath
like image 119
oskarko Avatar answered Nov 15 '22 19:11

oskarko


Don't use self.tableView in your cellForRowAtIndexPath method (or any of the other data source and delegate methods). Use the tableView parameter.

This is really important when your data source and delegate methods are being used for more than one table view.

like image 24
rmaddy Avatar answered Nov 15 '22 21:11

rmaddy


Another possibility is that you may be accidentally using dequeueReusableCell method more than once within the same tableView.

For example you may also be using it in didSelectRowAt or didEndDisplaying cell by mistake.

like image 22
SergPanov Avatar answered Nov 15 '22 21:11

SergPanov


In my case i was calling "cellForRowAt indexPath" while the table was reloading because of another parallel process. Then i implemented flag and delayed the "cellForRowAt indexPath" while "cellForRowAt indexPath" method is working. So it wont be load in parallel for same cell

like image 29
ergunkocak Avatar answered Nov 15 '22 21:11

ergunkocak