首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS,快速- UICollectionReusableView不工作

IOS,快速- UICollectionReusableView不工作
EN

Stack Overflow用户
提问于 2018-01-28 09:46:37
回答 2查看 3.3K关注 0票数 1

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

故事板

On device

我下面的代码不工作,打印(“ssssss”)不显示。

代码语言:javascript
复制
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()

代码语言:javascript
复制
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-28 09:53:43

您应该实现此委托方法

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    referenceSizeForHeaderInSection section: Int) -> CGSize {

    return CGSize(width: collectionView.bounds.width, height: 100)
}

不要忘记在故事板中设置您的delegatecollectionView

票数 1
EN

Stack Overflow用户

发布于 2019-09-27 08:01:27

如果要通过. .xib/storyboard配置自定义视图,则不需要在viewDidload中添加以下方法:

代码语言:javascript
复制
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")

选择情节提要中的视图并转到标识检查器,并链接Class nad Module

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

并确保在代码中添加该方法:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48485024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档