也许有人可以帮助我。我想将新的表视图行添加到tableView
。如果您按右上角的加号按钮,会弹出一个警告,您可以输入一些文本,此文本将被添加到表格中。
当我按下加号按钮时,我总是收到错误
(SIGABRT:'NSInvalidArgumentException',原因:' * - [__ NSArrayM insertObject:atIndex:]:object不能为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 :(得分:0)
我修改了你的代码。请检查一下。
[_objects insertObject:_shopping atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
在完成警报视图输入之前,您正在添加对象。所以在alertview compltion中插入对象。
- (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)
您正在向_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.