0

I have a collectionview with 3 sections, but there is no item in first section, when i'm using

let indexPath = IndexPath(row: row, section: section)

the project will crash, so what should i input when there is no items for this, is there any function like

let indexPath = IndexPath(section: section)
Gaga
  • 21
  • 2
  • 1
    You can't create indexPath for non-existing element. Where are you using this indexPath? You probably should check if collectionView.numberOfElements(inSection: section) > 0 before attempting to do whatever you're doing. – pcz Jul 17 '19 at 10:02
  • But how do i get the indexPath of the header even if there is no element in that section – Gaga Jul 17 '19 at 10:10
  • 2
    @Gaga Collectionview view header and footer doesn't have indexpaths. Only cells have indexpaths – RajeshKumar R Jul 17 '19 at 10:24
  • @Gaga Ok, that's entirely different problem. Depending on your implementation, you might want to add an invisible (e.g. almost 0-height) item to empty section, so that all indexPath dependant function will work. Check out https://stackoverflow.com/questions/48975433/how-to-add-supplementary-header-view-for-an-empty-section-in-collectionview-cust – pcz Jul 17 '19 at 10:38

0 Answers0