0

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

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

}

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

4

2 回答 2

0

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

于 2012-10-17T05:13:48.823 回答
0

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

于 2012-10-22T16:23:10.267 回答