尝试为 UICollectionView Swift 添加 header 时出错
Error trying to add header for UICollectionView Swift
我在尝试将 header 添加到我的 collection 视图时遇到问题。这是代码的相关部分:
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的子类。在 运行 这段代码并试图查看它的样子之后,我得到了这个错误:
the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader, {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (>)
(null)
我觉得你的条件有问题。
您收到的消息告诉您您没有将补充视图从 collectionView 中取出。所以问题在于您 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
returns 创建(初始化)视图的部分。
您的集合视图中的所有视图都必须从中取出。您可以在 documentation 中阅读更多内容。
我在尝试将 header 添加到我的 collection 视图时遇到问题。这是代码的相关部分:
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的子类。在 运行 这段代码并试图查看它的样子之后,我得到了这个错误:
the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader, {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (>) (null)
我觉得你的条件有问题。
您收到的消息告诉您您没有将补充视图从 collectionView 中取出。所以问题在于您 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
returns 创建(初始化)视图的部分。
您的集合视图中的所有视图都必须从中取出。您可以在 documentation 中阅读更多内容。