views:

1225

answers:

3

Hi,

Everyone keep writing about deleting a section. Well, I can't seem to get one added.

Currently, I am trying like this (which fails with NSInternalInconsistencyException):

UITableView *tv = (UITableView *) self.tableView;

if ([tv numberOfSections] == 1)
{
     [tv beginUpdates];
     [tv insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];

     NSLog(@"Inserted.. Brace for impact.");
     [tv endUpdates];
}

NSLog(@"Section count after update: %d", [tv numberOfSections]); // Never reached

If I am correct, inserting a section with index 0 should place it at the top, bumping all the other sections down, right? Well, if I write out the numberOfSections right after the insertSections, there appears to be no change in the number of sections.

Any ideas?

Johan

+1  A: 

Did you also update your data source? You can't just update the table view without also updating the underlying data.

Stephen Darlington
Yes.. hm. Yes I do, still the same exception. Thanks anyway.
Gatada
+1  A: 

You need to update numberOfSectionsInTableView message of UITableViewDataSource class.

Pablo Santa Cruz
Well, I do a [item count] to see if there is a reason to increase the sections number, if any items are added, it returns 2, otherwise 1.Or did you mean need to actively message numberOfSectionsInTableView?
Gatada
No. I didn't mean actively messaging numberOfSectionsInTableView. I meant what you apparently are already doing: returning the right number of sections on that message implementation.
Pablo Santa Cruz
+1  A: 

Yes, thanks to both of you.

After some juggling, I finally managed to get it working. It was a combination of both your suggestions. The new data was never inserted, but also I did not have to increase the row count for the first item inserted, but only the second.

Gatada