0
votes

I'm having some issues with my beginupdates and endupdates from my UITableView. I want to use this functions to animate my tableview instead of using reloadData that i was using till now. It always give a problem when i do it with BeginUpdates and complains about this:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

So there is a problem with my numberOfRowInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    int returnInt;

    if (section == 0) {
        if ([self datePickerIsShown]){
            returnInt = 3;
        }else{
            returnInt = 2;
        }
    }else if([[[[self theNewGame] _dobbelstenen] objectAtIndex:section - 1] diceSoort] == ENUMOgen){
        returnInt = 1;
    }else{
        returnInt = [[[[[self theNewGame] _dobbelstenen] objectAtIndex:section - 1.0] optiesDobbelsteen] count] + 1.0;
    }

    return returnInt;
}

And i'm calling it on this way:

if (indexPath.section == 0 && indexPath.row == 1) {
        self.datePickerIsShown = ! self.datePickerIsShown;
        [tableView beginUpdates];
        [tableView endUpdates];
    }

What's the problem?

Kind regards

2

2 Answers

3
votes

You've got to tell the table view what you changed so it knows how to animate. For example, if you had 2 items and you insert another, the table view would only know that you've now got 3 items. You need to tell it where you inserted the item. You do this by calling the following between your beginUpdates and endUpdates:

– insertRowsAtIndexPaths:withRowAnimation:

So, for example, if you're inserting the third item at the end of section 1, your code would look something like this:

NSIndexPath *insertedIndexPath = [NSIndexPath indexPathForRow:2 inSection:1];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[insertedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

Several related APIs are provided for other types of updates, deletes, moves, etc.:

– deleteRowsAtIndexPaths:withRowAnimation:
– moveRowAtIndexPath:toIndexPath:
– insertSections:withRowAnimation:
– deleteSections:withRowAnimation:
– moveSection:toSection:

You may also find my TLIndexPathTools library useful. It does this stuff automatically.

3
votes

You are changing the number of rows in the table view data source from 2 to 3.

if ([self datePickerIsShown]) {
    returnInt = 3;
} else {
    returnInt = 2;
}

You must update the table view so the table view and the table view data source agree on the number of rows.

if (indexPath.section == 0 && indexPath.row == 1) {
    self.datePickerIsShown = ! self.datePickerIsShown;
    [tableView beginUpdates];
    if ([self datePickerIsShown])
        [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
    else
        [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}

NOTE: Because you only have 1 change to your table view, you don't need begin and end updates. You can just call the 1 insert or delete that you need.

if (indexPath.section == 0 && indexPath.row == 1) {
    self.datePickerIsShown = ! self.datePickerIsShown;
    if ([self datePickerIsShown])
        [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
    else
        [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
}