簡體   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