0

当我尝试插入新条目时,我的应用程序崩溃了。我之前收到了这个错误,我无法修复它。任何人都可以帮忙吗?

这是我在控制台中显示的错误:

2013-09-14 15:41:00.370 Probation App[9919:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0xd7b81 0x33eb228d 0x33f34f81 0x328f6277 0x31fbd5df 0x31fbd291 0x31fbbf01 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0x542fd 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception

这是我的代码:

#pragma mark UITableViewDataSource Methods

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
    if( nil == cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (indexPath.row <self.probationers.count)
    {
        Probationer *thisProbationer = [self.probationers objectAtIndex:indexPath.row];
        cell.textLabel.text = thisProbationer.probationerName;
        cell.detailTextLabel.text = thisProbationer.probationerID;
    }
    else
    {
        cell.textLabel.text = @"Add Probationer";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

-(NSInteger)tableView: (UITableView *)tv numberOfRowsInSection:(NSInteger)section
{

    NSInteger count = self.probationers.count;
    if(self.editing)
    {
        count = count +1;
    }

    return count;

    //return [self.probationers count];
}

//Deleting an Entry

-(void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle) editing forRowAtIndexPath: (NSIndexPath *) indexPath
{
    if (editing == UITableViewCellEditingStyleDelete)
    {
        [self.probationers removeObjectAtIndex:indexPath.row];
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

#pragma mark UITableViewDelegate Methods

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Probationer *chosenProbationer = [self.probationers objectAtIndex:indexPath.row];
    ProbationerController *detailedViewController = [[ProbationerController alloc]init];
    detailedViewController.delegate = self;
    detailedViewController.currentProbationer = chosenProbationer;

    if (indexPath.row <self.probationers.count && !self.editing)
    {

        [self.navigationController pushViewController:detailedViewController animated:YES];
    }
    if (indexPath.row == self.probationers.count && self.editing)
    {

        AddProbationerController *addProbationer = [[AddProbationerController alloc] init];
        [self.navigationController pushViewController:addProbationer animated:YES];
    }

    [tv deselectRowAtIndexPath:indexPath animated:YES];

    //selectedIndexPath = indexPath;



    //[self.navigationController pushViewController:detailedViewController animated:YES];

}
4

2 回答 2

1

行数应始终来自数据源(您已注释掉的位//return [self.probationers count];)。不要试图只是添加到数字。添加到数据源,然后刷新表视图。

于 2013-09-14T22:50:16.803 回答
0

堆栈跟踪说明了一切。这绝对是因为你的self.probationers阵列。

你为什么不 NSLog 数组中的项目数和表格视图中的行/节数。当您尝试关联时indexPath.rowself.probationers您必须确保它们与元素的数量相匹配。

在访问数组元素时也要遵循基础知识(因为它们很容易出现异常)

  • Nil 检查访问数组。
  • 通过记录了解数组的确切计数。
  • 确保您尝试访问的对象多于可用数组索引。
于 2013-09-15T05:43:32.877 回答