Reputation: 6275
I've a UIViewController
with a UICollectionView
attached on it. The problem is that viewForSupplementaryElementOfKind
is called three times and not just one like in UITableView
.
Ideas ?
Upvotes: 3
Views: 2624
Reputation: 551
i do not know exactly why this happened, but i have a weird solution. before you return the UICollectionReusableView in the viewForSupplementaryElementOfKind methord,check the num of datasource,just like this
if kind == CHTCollectionElementKindSectionHeader
{
if let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: String(ImageDetailHeaderView), forIndexPath: indexPath) as? ImageDetailHeaderView
{
let bannerViewModel=ImageDetailHeaderVM(requestUrlStr: viewModel.headerRequestUrlStr, requestParams: viewModel.headerRequestParams)
if vm.cellModelList.count>0
{
***// only the datasource is not empty ,config the SupplementaryView***
header.vm=bannerViewModel
header.tagClickedHandler=tagClickedHandler
header.shareButtonClicked=shareButtonClicked
header.delegate=self
}
return header
}
}
Upvotes: 1
Reputation: 59
This is actually going to happen N times depending on whether you're reloading the collection view or scrolling the sections out of view and bringing them back. The viewForSupplementaryElementOfKind is going to get called whenever a section that has a header or footer gets displayed in the view
Upvotes: 0
Reputation: 42977
For UICollectionView
supplementary view's are nothing but Section Header
and Section Footer
. viewForSupplementaryElementOfKind
called two times for each section if you have already registered Classes for UICollectionElementKindSectionHeader
and UICollectionElementKindSectionFooter
. So the calling of viewForSupplementaryElementOfKind is depending on the number of the sections and availability of Section header/footer
Upvotes: 1