2016-07-08 19 views
0

Vielleicht kann mir jemand helfen. Ich möchte eine neue Tabellenansicht Zeile zu einem tableView hinzufügen. Wenn Sie die Plus-Taste in der oberen rechten Ecke drücken, erscheint eine Warnung und Sie können Text eingeben, und dieser Text wird zur Tabelle hinzugefügt.Hinzufügen von TableView-Zeilen mit AlertAction

Wenn ich die Plus-Taste am Drücken ich immer erhalte eine Fehlermeldung

(SIGABRT: 'NSInvalidArgumentException', Grund: '* - [__ NSArrayM insert: atIndex:]: Objekt kann nicht Null sein' * Erstwurf Anrufstapel :)

Ich verstehe es nicht.

@property NSMutableArray *objects; 
@property NSString *shopping; 


- (void)insertNewObject:(id)sender { 
if (!self.objects) { 
    self.objects = [[NSMutableArray alloc] init]; 
} 
//call an alert Action 
UIAlertController *textEntry = [UIAlertController alertControllerWithTitle:@"new Item" message:@"" preferredStyle:UIAlertControllerStyleAlert]; 
[textEntry addTextFieldWithConfigurationHandler:^(UITextField *textField){ 
    textField.placeholder = NSLocalizedString (@"zb. pickle", @"cde"); 

}]; 
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
    UITextField *enteredText = textEntry.textFields.firstObject; 
    _shopping = enteredText.text; 



}]; 
[textEingabe addAction:okAction]; 
[self presentViewController:textEingabe animated:YES completion:nil]; 

[_objects insertObject:_shopping atIndex:0]; 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
//Not quite sure if this is the right code 
cell.textLabel.text = [_objects objectAtIndex:[indexPath row]]; 
return cell; 

Antwort

0

Ich habe Ihren Code geändert. Bitte überprüfe das.

[_objects insertObject:_shopping atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

Sie fügen ein Objekt hinzu, bevor Sie mit der Eingabe der Alarmansicht fertig sind. Also fügen Sie das Objekt in die Alertview-Komplikation ein.

- (void)insertNewObject:(id)sender { 
    if (!self.objects) { 
     self.objects = [[NSMutableArray alloc] init]; 
    } 
    //call an alert Action 
    UIAlertController *textEntry = [UIAlertController alertControllerWithTitle:@"new Item" message:@"" preferredStyle:UIAlertControllerStyleAlert]; 
    [textEntry addTextFieldWithConfigurationHandler:^(UITextField *textField){ 
     textField.placeholder = NSLocalizedString (@"zb. pickle", @"cde"); 

    }]; 
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     UITextField *enteredText = textEntry.textFields.firstObject; 
     _shopping = enteredText.text; 

     [_objects insertObject:_shopping atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    }]; 
    [textEingabe addAction:okAction]; 
    [self presentViewController:textEingabe animated:YES completion:nil]; 



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    //Not quite sure if this is the right code 
    cell.textLabel.text = [_objects objectAtIndex:[indexPath row]]; 
    return cell; 
+1

Diese auf die Frage keine vollständige Antwort. Bitte erklären Sie, was das Problem war und wie Sie es behoben haben, anstatt Arbeitscode –

+0

Oh verdammt, ich bin dumm. Danke vielmals. Es hat funktioniert – podoi17

+0

@ podoi17 Willkommen bro :-) –

0

Sie einfügen nil _objects

sollte es so etwas wie dieses:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
    UITextField *enteredText = textEntry.textFields.firstObject; 
    _shopping = enteredText.text; 
    [_objects insertObject:_shopping atIndex:0]; // insert object here 
}]; 

[textEingabe addAction:okAction]; 
[self presentViewController:textEingabe animated:YES completion:nil]; 

, wenn Sie einen Standardwert für _shopping haben?.

mag:

_shopping = @"default"; 
<some code> 
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
UITextField *enteredText = textEntry.textFields.firstObject; 
_shopping = enteredText.text; 
}]; 

[textEingabe addAction:okAction]; 
[self presentViewController:textEingabe animated:YES completion:nil]; 
[_objects insertObject:_shopping atIndex:0]; // this way _shopping will never be nil.