我正在嘗試爲我的collectionView創建具有動態高度的標頭。但是,當我實施「referenceSizeForHeaderInSection」,應用程序崩潰,但以下情況除外:CollectionView方法'referenceSizeForHeaderInSection'拋出異常
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindSectionHeader with identifier FeedHeader - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
下面是對的CollectionView頭的代碼:因爲我還沒有添加任何
func numberOfSections(in collectionView: UICollectionView) -> Int {
return feedArray.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width - 20 , height: 70)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 10, 10, 10);
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FeedHeader", for: indexPath) as! FeedHeader
return header
}
FeedHeader類是空的標籤頭尚未:
class FeedHeader: UICollectionReusableView
{
}
當我刪除referenceSizeForHeaderInSection實施,應用程序工作沒有任何問題。
已將頭部鏈接到故事板中的類。 可能是什麼問題?
在此先感謝。
當我刪除referenceSizeForHeaderInSection實現時,該應用可以正常工作。我不這是註冊頭的問題。 – Vyshakh
你試過了上面的代碼嗎? @Vyshakh –
欲瞭解更多的澄清閱讀此https://stackoverflow.com/a/29656061/8432814回答 –