문제

My tableview uses in-place editing and adding. A new row is added when the edit button is pushed and this new row can be edited and commited. When commiting the new row into the tableview, the new row is added and the blank cell is moved down which can subsequently be edited and another row added again. However when attempting to add another row during the same editing session I get an error:

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

I was getting this error before when adding only a single row but after following a tutorial on in-place editing I thought I had it solved. Here are some relevant methods:

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

}

How can I resolve this issue so the user is able to add row after row in a single editing "session"?

도움이 되었습니까?

해결책 2

In the end I had to change a lot and add a manual count for the number of rows in the tablview that was changed after each update. It's not the way I like to code but it works and I couldn't work out what was going wrong.

다른 팁

You should place the insert/delete/select lines between the [self.tableview beginUpdates]; and [self.tableview endUpdates];

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top