我在尝试向集合视图添加标头时遇到问题。这是代码的相关部分:
collectionView?.register(ProfileHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: Identifier.header)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: view.frame.width, height: 262.5)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if member == nil {
return UICollectionReusableView()
}
switch kind {
case UICollectionElementKindSectionHeader:
let profileHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.header, for: indexPath) as! ProfileHeaderView
if indexPath.section == Section.header {
profileHeaderView.config(...)
return profileHeaderView
} else {
let reusabelView = UICollectionReusableView()
let seperatorLine = UIView.seperatorLine()
reusabelView.addSubview(seperatorLine)
seperatorLine.bottomAnchor.constraint(equalTo: reusabelView.bottomAnchor).isActive = true
seperatorLine.leftAnchor.constraint(equalTo: reusabelView.leftAnchor).isActive = true
seperatorLine.centerXAnchor.constraint(equalTo: reusabelView.centerXAnchor).isActive = true
reusabelView.backgroundColor = Color.white
return reusabelView
}
default:
return UICollectionReusableView()
}
}
ProfileHeaderView是UICollectionReusableView的子类。运行此代码并尝试查看它的外观后,我收到此错误:
从-collectionView:viewForSupplementaryElementOfKind返回的视图:atIndexPath(UICollectionElementKindSectionHeader,{length = 2,path = 0 - 0})未通过调用-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:或者是nil(>)来检索 (空)
答案 0 :(得分:0)
我觉得你的情况有问题。
告知您收到的消息,告知您没有从collectionView中将您的补充视图出列。问题在于func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
返回创建(inited)视图的部分。
集合视图中的所有视图都必须从中出列。您可以在documentation中阅读更多内容。