尝试插入时TableView崩溃

时间:2013-09-14 22:47:17

标签: ios uitableview

当我尝试插入新条目时,我的应用崩溃了。我之前收到此错误,但我无法修复它。有人可以帮忙吗?

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

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

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

堆栈跟踪说明了一切。这肯定是因为你self.probationers数组。

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

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

  • 无需检查访问阵列。
  • 通过记录了解数组的确切数量。
  • 确保您尝试访问的对象多于可用的数组索引。
相关问题