简体   繁体   English

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

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

self.alphabets is my data source which contains all the alphabets. 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);
}

This is the code, which i am using to add or insert rows/cells in tableview by using the data already present. 这是代码,我正在使用已存在的数据在tableview中添加或插入行/单元格。

But i want to add or insert a new row/cell by taking the user input. 但是我想通过接受用户输入来添加或插入新的行/单元格。 How to do it. 怎么做。

I have tried to use UIAlertController. 我试图使用UIAlertController。 Is that a right approach? 这是正确的方法吗? But i failed. 但是我失败了。 Someone please help me. 有人请帮助我。

Also, i have a doubt about inserting multiple rows at a time. 另外,我对一次插入多行存有疑问。 How to achieve it. 如何实现。

Here is the code which i am using to insert multiple rows at a time. 这是我一次用来插入多行的代码。

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

Here is the exception i am getting. 这是我得到的例外。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (2 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' ***由于未捕获的异常'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);
}

Using the above code, I am able to add or insert new rows or cells with user input. 使用上面的代码,我可以通过用户输入添加或插入新的行或单元格。

I used a UIAlertController to complete this task. 我使用UIAlertController完成此任务。 I added a UITextfield to the alert controller and can now get input from it. 我将UITextfield添加到警报控制器,现在可以从中获取输入。 It in turn adds it to the datasource and the reloaded UITableview. 依次将其添加到数据源和重新加载的UITableview中。

self.userInput is an string used to store the user input given through a textfield in an alert. self.userInput是一个字符串,用于存储警报中通过文本字段给出的用户输入。

If anyone thinks that this code can be improved, then let me know. 如果有人认为可以改进此代码,请告诉我。 I am always willing to improve myself. 我一直乐于提高自己。

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

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