首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionReusableView中的标签始终为空

UICollectionReusableView中的标签始终为空
EN

Stack Overflow用户
提问于 2017-04-16 11:02:44
回答 1查看 434关注 0票数 2

我有一个自定义的UICollectionReusableView,HeaderCollectionReusableView,我正在代码中使用它。而它又包含一个UILabel。当我使用标签时,由于某种原因,它是空的。我首先注册这个类,然后尝试使用出队来调用它。我遗漏了什么?

代码语言:javascript
复制
override func viewDidLoad(){
    super.viewDidLoad()

    collectionView?.register(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: colReusableViewIdentifier)

    self.collectionView?.collectionViewLayout = UICollectionViewFlowLayout()
}

@objc override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let headerReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: colReusableViewIdentifier, for: indexPath) as! HeaderCollectionReusableView
    headerReusableView.headerLabel.text = "Reacshn"

    return headerReusableView
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-16 11:49:50

看一下代码,我假设定制的可重用视图不是使用xib创建的。最好的选择是

  1. 使用xib创建自定义的可重用视图。使用以下函数(如func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)

)在viewDidLoad中注册

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43433282

复制
相关文章

相似问题

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