0
votes

I have a UITableView with a C++ object being the datasource, so there isn't any kind of NSArray. I'm getting a crash whenever I want to insert a row to the end of the existing list

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]

It crashes on [tableView endUpdates]. However I don't have any NSArray or any code calling objectAtIndex in my implementation. I don't know why I'm crashing.

I only have a single section and always add the new row to the end of section 0. The number of rows returned are correct and therefor fully synch.

In the logs I see, [tableView endUpdates] calls to numberOfRowsInSection which correctly returns a count of the previous count plus one. However, it never calls to cellForRowAtIndexPath again and unable to get past [tableView endUpdates];

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"...");

    ........


    NSArray *insertIndexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:oldCount + i inSection:[indexPath section]], nil];

    NSLog(@"Insert: %d %d %d", i, (int) oldCount, (int) newCount);

    [tableView beginUpdates];        
    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];                
    [tableView endUpdates];

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int i = _reader->allRead() ? _reader->getWindowCount() : _reader->getWindowCount() + 30;    
    return i;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{   
    return 1;
//    return __results__ ? 1 : 0;
}
2
problem in this line only NSArray *insertIndexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:oldCount + i inSection:[indexPath section]], nil];Narayana
I'm adding to the last element and in the same section (0). Even I changed oldCount + i to 1 or 2, I get the same results.SmallChess
I faced this problem before NSIndexPath *indexpath_delete = [NSIndexPath indexPathForRow:oldCount + i inSection:[indexPath section]];Narayana
[tableView beginUpdates]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_delete] withRowAnimation:UITableViewRowAnimationNone]; [tableView endUpdates];Narayana
try like above may be help to you..Narayana

2 Answers

0
votes

as you are updating the tableview you must update the datasource too. in your code no datasource updating method is shown so i thinks that that should be taken in account.

0
votes

Try [[self tableView] reloadData] instead of [[self tableView] endUpdates]