George Friday
George Friday

Reputation: 585

TableView Crashes when Inserting

I keep getting the following error when trying to insert a row:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0x8d18f 0x33fee5a9 0x33edb0c5 0x33edb077 0x33edb055 0x33eda90b 0x33edae01 0x33df9421 0x31fbd6cd 0x31fbb9c1 0x31fbbd17 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0xa49d 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception

Does anyone know what's going on here?

Here is my code:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        Caseload *deletedCaseload = [self.caseload objectAtIndex:indexPath.row];
        [self.caseload removeObject:deletedCaseload];

        [self.caseloadTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else if
        (editingStyle == UITableViewCellEditingStyleInsert)
    {


        Caseload *copiedEntry = [self.caseload objectAtIndex:indexPath.row];
        Caseload *newEntry = [[Caseload alloc]init];
        newEntry.name = copiedEntry.name;
        newEntry.address = copiedEntry.address;
        newEntry.phoneNumber = copiedEntry.phoneNumber;
        newEntry.identNumber = copiedEntry.identNumber;

        [self.caseload addObject:newEntry];
        [self.caseloadTableView insertRowsAtIndexPaths:
                    [NSArray arrayWithObject:indexPath]
                                              withRowAnimation:UITableViewRowAnimationRight];
    }
}

Upvotes: 0

Views: 67

Answers (2)

iPatel
iPatel

Reputation: 47059

This types of error generate whenever you try to get object/value of an array at totalIndex + 1 such like for example if your array has 4 value (Make sure index start with 0) and you try to fetch value such like
[Myarray objectAtIndex:4] at that time this type of error occur.

So best why is use breakPoint and find out your problem by help of my suggestion.

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

The error is "NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'". That means you're calling "objectAtIndex:" somewhere with an index of 2, and your NSArray only has entries at index 0 and index 1.

You need to figure out where you are setting the bogus index, which I suspect might start in your "numberOfRowsInSection:" method.

Upvotes: 0

Related Questions