0

也许有人可以帮助我。我想将一个新的表视图行添​​加到tableView. 如果您按下右上角的加号按钮,则会弹出一个警报,您可以输入一些文本,此文本将添加到表格中。

当我按下加号按钮时,我总是收到错误消息

(SIGABRT :'NSInvalidArgumentException',原因:' * -[__NSArrayM insertObject:atIndex:]: object cannot be nil' * First throw call stack:)

我不明白。

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

2 回答 2

0

我已经修改了你的代码。请检查一下。

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

在完成警报视图输入之前,您正在添加对象。所以在 alertview 完成中插入对象。

- (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;
于 2016-07-08T09:21:55.807 回答
0

您正在插入 nil 到_objects

它应该是这样的:

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

除非你有一个默认值_shopping?。

喜欢:

_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.
于 2016-07-08T09:37:33.510 回答