繁体   English   中英

如何使用用户输入在UITableView中添加/插入新行

[英]How to add/insert a new row in UITableView using user input

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);
}

这是代码,我正在使用已存在的数据在tableview中添加或插入行/单元格。

但是我想通过接受用户输入来添加或插入新的行/单元格。 怎么做。

我试图使用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移出)。

- (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是一个字符串,用于存储警报中通过文本字段给出的用户输入。

如果有人认为可以改进此代码,请告诉我。 我一直乐于提高自己。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM