插入时TableView崩溃

时间:2013-09-14 08:59:41

标签: ios xcode uitableview

尝试插入行时,我一直收到以下错误:

*** 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

有谁知道这里发生了什么?

这是我的代码:

-(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];
    }
}

2 个答案:

答案 0 :(得分:1)

每当您尝试在 totalIndex + 1 获取数组的对象/值时,就会生成此类错误,例如,如果您的数组具有 4 (确保索引以 0 开头),然后尝试获取类似于的值 当时发生[Myarray objectAtIndex:4]这种类型的错误。

所以最好的原因是使用breakPoint并根据我的建议找出你的问题。

答案 1 :(得分:0)

错误为“NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'”。这意味着你在索引为2的地方调用“objectAtIndex:”,而你的NSArray只有索引0和索引1的条目。

你需要弄清楚你在哪里设置虚假索引,我怀疑这可能是从你的“numberOfRowsInSection:”方法开始的。

相关问题