ios - 就地 UITableView 编辑 : Adding a row crash

标签 ios uitableview crash edit-in-place

我的表格 View 使用就地编辑和添加。当按下编辑按钮时会添加一个新行,并且可以编辑和提交该新行。将新行提交到 TableView 中时,将添加新行并将空白单元格向下移动,随后可以对其进行编辑并再次添加另一行。但是,当尝试在同一编辑 session 期间添加另一行时,我收到错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update'

之前,我在仅添加一行时遇到了此错误,但在遵循就地编辑教程后,我认为我已经解决了这个问题。以下是一些相关方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    int count = [items count];
    if(self.editing)count++;
    return count;

}



- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    [keyphraseTextField setEnabled:YES];

    NSArray *paths = [NSArray arrayWithObject:
                      [NSIndexPath indexPathForRow:[items count] inSection:0]];
    if (editing)
    {
        [[self tableView] insertRowsAtIndexPaths:paths
                                withRowAnimation:UITableViewRowAnimationTop];
    }
    else {
        [[self tableView] deleteRowsAtIndexPaths:paths
                                withRowAnimation:UITableViewRowAnimationTop];
    }
}



- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self deleteItemAtIndexPath:indexPath];

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert) {

        NSIndexPath *thePath = [NSIndexPath indexPathForRow:[items count] inSection:0];

        DBAccess *dbaccess = [[DBAccess alloc] init];

        NSInteger keyphraseCount = [dbaccess getDomainKeyphraseCount:domain_id];
        NSInteger keyphraseCountLimit = [dbaccess getDomainKeyphraseCountLimit:domain_id];

        [dbaccess closeDatabase];

        [dbaccess release];

        if(![keyphraseTextField.text isEqualToString:@""] && keyphraseCount<keyphraseCountLimit){

            [self insertItemAtIndexPath:thePath];

            [self readItems];

            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:thePath] withRowAnimation:UITableViewRowAnimationLeft];

            [self makeNSURLConnection];

        }else{

            if([keyphraseTextField.text isEqualToString:@""]){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please insert Keyphrase." delegate:self cancelButtonTitle:@"Not Now." otherButtonTitles:@"Upgrade!", nil];
                [alert setTag:10];
                [alert show];
                [alert release];
            }
            if(keyphraseCount>=keyphraseCountLimit){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Kindly upgrade your subscription to add more keyphrases!" delegate:self cancelButtonTitle:@"Not Now." otherButtonTitles:@"Upgrade!", nil];
                [alert setTag:10];
                [alert show];
                [alert release];
            }

        }

    }

}



- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;

    if (self.editing && indexPath.row == ([items count])) {

        return UITableViewCellEditingStyleInsert;

    } else {

        return UITableViewCellEditingStyleDelete;

    }

    return UITableViewCellEditingStyleNone;

}

如何解决此问题,以便用户能够在单个编辑“ session ”中逐行添加?

最佳答案

您应该将插入/删除/选择行放在 [self.tableview beginUpdates];[self.tableview endUpdates];

之间

关于ios - 就地 UITableView 编辑 : Adding a row crash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915674/

相关文章:

php - 执行快速入门时未找到 Twilio token

iphone - CATransform3D 变换

ios - 删除使用 telprompt ://从 iOS 应用程序调用时弹出的警报

iphone - 在 UITableViewCell 的父类中使用 XIB

c - 中止陷阱 : 6 in C Program

android - 应用程序仅在 android 4.4.2 上崩溃

ios - 通过标签编辑 UITableViewCell

IOS Tableview 支持全屏和横屏

ios - 如何在 Swift3 中使用分隔符连接 NSMutableArray 的对象

c - 如何执行逐行崩溃调试来处理 c 中的 "has stopped working"错误?