就地 UITableView 编辑:添加行崩溃



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

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

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

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

最后,

我不得不进行大量更改,并为每次更新后更改的tablview中的行数添加手动计数。这不是我喜欢编码的方式,但它有效,我无法弄清楚出了什么问题。

相关内容

  • 没有找到相关文章

最新更新