2017-02-17 4 views
1

auf dem ersten Bildschirm Benutzer Daten eingeben & Anzeige in Second ViewController mit prepareforsegue Methode.auf dem zweiten Bildschirm Benutzer wählt mehrere Zeilen zum Löschen dann wieder wählt Benutzer verbleibende Zeilen zum Löschen Anwendung crash.Here ist mein CodeAnwendung Absturz bei der Verwendung von UITableView

* Beenden app aufgrund nicht abgefangene Ausnahme 'NSRangeException', Grund: '* - [NSMutableArray removeObjectsAtIndexes:]: Index 5 in Index über Grenzen gesetzt [0 .. 4]'

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


} 

Bitte schlagen Sie mir vor. Danke.

+0

zeigen den Absturzbericht –

+0

*** App beenden aufgrund nicht abgefangene Ausnahme 'NSRangeException', Grund: ‚*** - [NSMutableArray removeObjectsAtIndexes:]: Index 5 in Index über Grenzen gesetzt [0 .. 4] ' –

+0

überprüfen Sie einmal diese Zeile in Ihrem Code '[mAryValue removeObjectsAtIndexes: indicesToDelete];', versucht die Datenquelle tableview auf den 5/4 Index zuzugreifen. –

Antwort

1

[NSMutableArray removeObjectsAtIndexes:]: Index 5 in Index über Grenzen gesetzt [0 .. 4]‘

Sie versuchen, ein Objekt in einem Index auf Ihrem array zu entfernen, die nicht vorhanden ist . Wie der Fehler sagt, entfernen Sie objectAtIndex:5, aber Ihr Array hat nur 4 Elemente.

[mAryValue removeObjectsAtIndexes:indicesToDelete]; 
+0

@AhmedSahib Er sollte den richtigen Index zu starten, nicht mit magischen Zahlen wie Sie vorschlagen. –

Verwandte Themen