2017-02-17 18 views
1

最初の画面でユーザーがデータ&を入力します.2番目のViewControllerでprepareforsegueメソッドを使用して表示されます。2番目の画面でユーザーが複数の行を選択してから削除します。 '* - [NSMutableArrayのremoveObjectsAtIndexes:]:境界を超えて設定されたインデックスのインデックス5 [0 .. 4]'アプリケーションUITableViewの使用中にクラッシュする

*によりキャッチされない例外 'NSRangeException'、理由にアプリを終了

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return mAryValue.count; 
} 



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * [email protected]"id1Cell"; 

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:strIdent]; 

    if (cell==nil) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdent]; 
    } 

    cell.textLabel.text=[mAryValue objectAtIndex:indexPath.row]; 

    return cell; 

} 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.editing) 
    { 
     return; 
    } 

    [_tblView deselectRowAtIndexPath:[_tblView indexPathForSelectedRow] animated:NO]; 
    UITableViewCell *cell = [_tblView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [mArySel addObject:indexPath]; 
    } 
    else if(cell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [mArySel removeObjectIdenticalTo:indexPath]; 
    } 
} 

-(void)getData:(NSMutableArray *)userValues 
{ 
    mAryValue=userValues; 
} 

- (IBAction)btnDelete:(id)sender 
{ 
     if (mAryValue==nil || mAryValue.count==0) 
     { 

      UIAlertController * alert= [UIAlertController 
              alertControllerWithTitle:@"Alert" 
              message:@"Please Enter Value" 
              preferredStyle:UIAlertControllerStyleAlert]; 

      UIAlertAction* ok = [UIAlertAction 
           actionWithTitle:@"OK" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 
            [alert dismissViewControllerAnimated:YES completion:nil]; 

           }]; 
      UIAlertAction* cancel = [UIAlertAction 
            actionWithTitle:@"Cancel" 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) 
            { 
             [alert dismissViewControllerAnimated:YES completion:nil]; 

            }]; 

      [alert addAction:ok]; 
      [alert addAction:cancel]; 

      [self presentViewController:alert animated:YES completion:nil]; 
     } 
    else 
    { 
     NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init]; 
     for (NSIndexPath *indexPath in mArySel) 
     { 
      [indicesToDelete addIndex:indexPath.row]; 
     } 

     if (!(indicesToDelete==nil) || !(indicesToDelete.count==0)) 
     { 
      [mAryValue removeObjectsAtIndexes:indicesToDelete]; 

     } 
     [_tblView reloadData]; 
    } 


} 

お願いします。 ありがとうございます。

+0

がキャッチされない例外により 'NSRangeException'、理由にアプリを終了***クラッシュレポート –

+0

を表示します' –

+0

あなたのコード '[mAryValue removeObjectsAtIndexes:indicesToDelete];'でこの行を一度チェックすると、tableviewデータソースは5/4のインデックスにアクセスしようとします。 –

答えて

1

[NSMutableArrayのremoveObjectsAtIndexes:]: 境界を越えて設定されたインデックスのインデックス5 [0 .. 4]」

あなたは存在しませんあなたのarrayのインデックスでオブジェクトを削除しようとしています。エラーメッセージにはobjectAtIndex:5が削除されていますが、配列には4つのアイテムしかありません。境界を越えて設定されたインデックスのインデックス5 [0 .. 4]: - [NSMutableArrayのremoveObjectsAtIndexes:]「***:

[mAryValue removeObjectsAtIndexes:indicesToDelete]; 
+0

@AhmedSahib彼は、あなたが示唆しているように、魔法の数字を実行するのではなく、正しいインデックスを取得する必要があります。 –

関連する問題