Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help to insert a new section in a TableView

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

like image 315
Johan Avatar asked Mar 30 '09 15:03

Johan


People also ask

How do I add a section in Swift?

To add a section around some cells, start by placing a Section around it, optionally also adding a header and footer. As an example, we could create a row that holds task data for a reminders app, then create a list view that has two sections: one for important tasks and one for less important tasks.

What is Tableview section?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text.


2 Answers

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.

like image 151
Johan Avatar answered Nov 14 '22 11:11

Johan


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

like image 27
Stephen Darlington Avatar answered Nov 14 '22 10:11

Stephen Darlington