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 に答える