就地TableView编辑:添加行崩溃

时间:2012-10-16 13:23:27

标签: ios uitableview crash edit-in-place

我的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;

}

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

2 个答案:

答案 0 :(得分:0)

您应该在[self.tableview beginUpdates];[self.tableview endUpdates];

之间放置插入/删除/选择行

答案 1 :(得分:0)

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