1

I am trying to setup an UIButton that click it would add a new section into my UICollectionView. I've searched for some examples online, but could make them work. Following is one of the answers which looks fine, but still not working for me.

Am I missing something? Could anyone provide some help? Many thanks in advance!

func buttonAction(sender:UIButton!) {
    var paths = [NSIndexPath]()
    paths.append(NSIndexPath(forRow: 0, inSection: 0))
    self.table.insertItemsAtIndexPaths(paths)
}
  • 1
    Where is your `numberOfSectionsInTableView:` method? This is the method that defines how many sections should be displayed. As an aside, your code looks wrong. It depends on how you've written your app but I would expect you to be appending a new array to the array that you use to count the number of sections in your tableview. The new array holds the rows in that section. I would expect a data structure like `array1->array2->dictionary` where `array1` returns the number of sections and `array2` returns the number of rows in a specific section. – Robotic Cat Aug 01 '15 at 16:46
  • Oops - where I wrote `Table View` read `Collection View`. – Robotic Cat Aug 01 '15 at 16:53
  • Thank you so much Robotic Cat! I have solved the problem following your advice. It turns out that in 'numberOfSectionsInCollectionView' I was returning a constant instead of the count of sections. – Gordon Vanderbilt Aug 01 '15 at 17:09

1 Answers1

0

You have to update the your table view's datasource and reload the table.

E.g., if your data source is an array with section headings and arrays of some objects that can provide the cell data, you would declare it like this:

var dataArray : [String: [CellObject]]?

Then you provide the usual data source things, e.g. in numberOfSectionsInTableView something like dataArray?.count; in numberOfRowsInSection something like dataArray[section]!.count etc.

The button would simply modify dataArray and then call reloadData on the table view (or alternatively, insert the section and adjust the data all within a beginUpdates-endUpdates bracket).

Mundi
  • 79,884
  • 17
  • 117
  • 140