SPatel
SPatel

Reputation: 4956

How to insert multiple sections in collectionview at once?

Actually i know how to insert one section at a time but i don't know how to insert multiple section at once?.

collectionView.insertSections(IndexSet(integer: array.count + 1))

how to insert multiple section at once?.

ex

var array = [3,4,2,1,6] // one section for one element

func numberOfSections(in collectionView: UICollectionView) -> Int {
   return array.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array[section]
}

//now i want to add three new sections [8,5,9]

Api doc:

Use this method to insert one or more sections into the collection view. This method adds the sections,

Upvotes: 0

Views: 634

Answers (1)

vadian
vadian

Reputation: 285150

An IndexSet can contain multiple indexes

collectionView.insertSections(IndexSet([2, 4, 7]))

Edit:

To insert 3 new sections at end of

var array = [3,4,2,1,6]

use

let startIndex = array.count
array.append(contentsOf: [8,5,9])
let endIndex = array.count
collectionView.insertSections(IndexSet(startIndex..<endIndex))

Upvotes: 2

Related Questions