2016-07-08 7 views
0

多分誰かが私を助けることができます。 tableViewに新しいテーブルビュー行を追加したいと思います。右上隅のプラスボタンを押すと警告がポップアップし、テキストを入力するとこのテキストがテーブルに追加されます。理由は、 'NSInvalidArgumentException':私はプラスボタンを押していた場合AlertAction付きTableView行を追加

私は常にエラーに

(SIGABRTを取得しています '* - [__ NSArrayM は、insertObject:atIndexは:]:オブジェクトがnilすることはできません' *まずスローコールスタック:)

私はそれを得るいけません。

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

答えて

0

私はあなたのコードを変更しました。それを確認してください。

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

アラートビューの入力が完了する前にオブジェクトを追加しています。したがって、alertview complotionの中にオブジェクトを挿入してください。あなたは_shoppingのデフォルト値を持っていない限り

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

:あなたは_objects

にnilを挿入している

- (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

これは質問に対する完全な答えではありません。問題の内容と、作業コードを掲示するのではなく修正した方法を説明してください。 –

+0

ああ私はばかです。どうもありがとう。それは働いた – podoi17

+0

@ podoi17ようこそbro :-) –

0

それはこのようなものでなければなりません?。

のような:

_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. 
関連する問題