Search code examples
iosswiftuitableviewsections

InsertSections function Swift


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.


Solution

  • 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