我想使集合视图标题,但它不工作。
故事板

On device

我下面的代码不工作,打印(“ssssss”)不显示。
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
print("sssssss")
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "mypageheader",
for: indexPath) as! MyPageHeaderView
headerView.backgroundColor = UIColor.blue;
return headerView
default:
assert(false, "Unexpected element kind")
}
}关于viewDidLoad()
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")发布于 2018-01-28 09:53:43
发布于 2019-09-27 08:01:27
如果要通过. .xib/storyboard配置自定义视图,则不需要在viewDidload中添加以下方法:
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")选择情节提要中的视图并转到标识检查器,并链接Class nad Module

然后转到属性检查器,将标识符ann:

并确保在代码中添加该方法:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeListTopCollectionReusableViewId", for: indexPath) as! HomeListTopCollectionReusableView
return header
}https://stackoverflow.com/questions/48485024
复制相似问题