0

I am using the viewForSupplementaryElementOfKind function to apply the header section from the uicollectionview controller. However, before the asynchronous parsing of the viewDidAppear API, the row index is loaded into the viewForSupplementaryElementOfKind function and becomes out of range. What should I do?

Here is my code...

    override func viewDidAppear(_ animated: Bool) {
        callVideo3API()
    }

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:
            let row1 = self.list[0]
            let row2 = self.list[1]
            let row3 = self.list[2]

    let headerSection = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! HeaderSection


        headerSection.nameLabel01.text = row1.nickname
        headerSection.nameLabel02.text = row2.nickname
        headerSection.nameLabel03.text = row3.nickname


        return headerSection

    default:

        assert(false, "Unexpected element kind")
    }
}
arunjos007
  • 4,105
  • 1
  • 28
  • 43
Travis
  • 29
  • 12
  • it came because of the data source may be empty. So, you must have to reload `collectionView` after getting data source . and also post your `numberOfSectionsInCollectionView` and `numberOfItemsInSection` method. – Hardik Shekhat Feb 25 '17 at 05:40

1 Answers1

1

You must wait until the callVideo3API() get completed. After successful completion of callVideo3API() you can reload the collection view to get the output. Please follow below steps

  1. Call method callVideo3API()
  2. Make CollectionView empty by returning zero through CollectionView DataSource [func numberOfSections(in collectionView: UICollectionView) -> Int, func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int ]
  3. (Optional) On the time of callVideo3API() execution you can a show an activity indicator on place of your CollectionView
  4. After successful completion of callVideo3API() you can reload CollectionView with corresponding DataSource Value. This time it will work without any fault :-) (If you put activity indicator don't forget to remove after successful api call)
arunjos007
  • 4,105
  • 1
  • 28
  • 43