self.alphabets是我的数据源,它包含所有的字母。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ [tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.alphabets removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
[self.alphabets insertObject:[self.alphabets objectAtIndex:indexPath.row] atIndex:[self.alphabets count]-1];
NSIndexPath * path1 = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSArray * index = [NSArray arrayWithObjects:path1, nil];
[self.tableView insertRowsAtIndexPaths:index withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
NSLog(@"%@",self.alphabets);
}这是代码,我用它来使用已经存在的数据在表视图中添加或插入行/单元格。
但是,我想通过接受用户输入来添加或插入一个新的行/单元格。怎么做。
我试过使用UIAlertController。这是正确的方法吗?但我失败了。谁来帮帮我。
此外,我对一次插入多个行有疑问。如何实现这一目标。
下面是我一次用于插入多个行的代码。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleInsert) {
[self.alphabets insertObject:[self.alphabets objectAtIndex:indexPath.row] atIndex:[ self.alphabets count]-1];
NSIndexPath * path1 = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSIndexPath * path2 = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
NSArray * index = [NSArray arrayWithObjects:path1,path2, nil];
[self.tableView insertRowsAtIndexPaths:index withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
NSLog(@"%@",self.alphabets);
}这是我得到的例外。
*终止应用程序,由于未定义的异常'NSInternalInconsistencyException',原因:“无效更新:节0中的无效行数。更新(11)之后包含在现有节中的行数必须等于更新(10)之前包含在该节中的行数,加上或减去从该节插入或删除的行数(2插入,0删除),加上或减去移动到或移出该节的行数(0移动,0移出)。”
发布于 2016-04-13 10:37:21
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Inserting a new row" message:@"Enter text below to add it to table view" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSArray * tfArray = alert.textFields;
UITextField * tf = [tfArray objectAtIndex:0];
self.userInput = tf.text;
NSLog(@"%@",self.userInput);
[self.alphabets insertObject:self.userInput atIndex:indexPath.row];
[tableView reloadData];
}];
[alert addAction:ok];
[self presentViewController:alert animated:NO completion:nil];
}
NSLog(@"%@",self.alphabets);
}使用上述代码,我可以添加或插入具有用户输入的新行或单元格。
我使用一个UIAlertController来完成这个任务。我向警报控制器添加了一个UITextfield,现在可以从它获得输入。它反过来将其添加到数据源和重新加载的UITableview中。
self.userInput是一个字符串,用于在警报中存储通过textfield提供的用户输入。
如果有人认为这个代码可以改进,那就告诉我。我总是愿意提高自己。
https://stackoverflow.com/questions/36576925
复制相似问题