简体   繁体   中英

NSInternalInconsistencyException in insertRowsAtIndexPaths

I'm trying to implement insertRowsAtIndexPaths for my table view.

Before this implementation, I used to call [self.tableView reloadData] after user hit end of table. This method kinda impacted my app quality for lots of row and lots of scrolling.(freezing each time before reloading data)

So I used this code instead:

p.lastid = appRecord.ids;
[p main];
dispatch_async(dispatch_get_main_queue(), ^(void)
        {                                    
         [self.tableView beginUpdates];
         NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
         [self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
         [self.tableView endUpdates];

          });
      });
    }
    return cell;
}

The old working code was:

  NSArray *temp =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
  self.entries = temp;
  [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

p.appRecordList is an NSArray and the new row item is from hayoola fetch.

For numberOfRowInSection I use this:

#define kCustomRowCount     10
@property (nonatomic, assign) int intindex;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger count = [self.entries count];
    if (count == 0)
    {
        return kCustomRowCount;
    }
    return count;
}

Also there is no cellforRowAtIndexPath in my code. I should mention that my program will fetch 10 row each time from serve r.

This is the Console error code:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 10 into section 0, but there are only 10 rows in section 0 after the update'*

edit :(change due to answer)

after changing the update method to this :

[self.tableView beginUpdates];
NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp_1;
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

and insertRowsAtIndexPaths to this :

    NSUInteger count = [self.entries count];
    return count;

this error appears :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (20) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'*

You need to ensure that the additional record(s) have been added to self.entries before the call to endUpdates . The exception is telling you that you tried to add an 11th row, but numberOfRowsInSection returned 10

You need ensure that you insert the same number of rows as elements added to your array

Your update method should be

[self.tableView beginUpdates];

NSMutableArray *newIndices=[NSMutableArray new];

for (id record in p.appRecordList) {
   [self.entries addObject:record];
   [newIndicies addObject:[NSIndexPath indexPathForRow:self.entries.count]];
}


[self.tableView insertRowsAtIndexPaths:newIndices withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM