Reputation: 1581
My goal is to insert a section upon a click and insert 4 rows into this section.
I'm already familiar with how to use InsertRowsAtIndexPaths
and inserting rows into one section makes no problem.
But when it comes to inserting new sections, it's tricky and the apple documentation doesn't explain it fully.
Here is the code i use for inserting the rows
self.tableView!.beginUpdates()
var insertedIndexPaths: NSMutableArray = []
for var i = 0; i < newObjects.count + 1; ++i {
insertedIndexPaths.addObject(NSIndexPath(forRow: initialCount + i, inSection: sectionsnumbers)) }
self.tableView?.insertRowsAtIndexPaths(insertedIndexPaths as [AnyObject], withRowAnimation: .Fade)
self.tableView!.endUpdates()
Any examples, insights are very appreciated.
Thank you.
Upvotes: 1
Views: 2547
Reputation: 644
Inserting sections is different than inserting rows. If you want to insert rows you need to call tableView.beginUpdates() first and table.endUpdates() after done, otherwise it will throw some data inconsistency exception. On the other hand, when inserting sections you don't have to do anything, just reflect the change in your datasource like numberOfSectionsInTableView.
Upvotes: 0
Reputation: 1003
use var sectionNumbers:NSMutableArray = ({"object":"object"}) //etc whatever object you want
Now use this function
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return sectionNumbers.count}
and add object in sectionNumbers and callThis method method where you want to add section
tableView.reloadData()
It will help
Upvotes: 0
Reputation: 118651
When you insert a section, its data is reloaded. You don't need to tell the tableview about all the new rows individually; it will just ask the data source for the number of rows in the new section.
See this document for more information.
Upvotes: 1