简体   繁体   中英

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

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

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. 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).'

- (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. I added a UITextfield to the alert controller and can now get input from it. It in turn adds it to the datasource and the reloaded UITableview.

self.userInput is an string used to store the user input given through a textfield in an alert.

If anyone thinks that this code can be improved, then let me know. I am always willing to improve myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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