【问题标题】:Header in CollectionView not Displaying iOS SwiftCollectionView 中的标题不显示 iOS Swift
【发布时间】:2016-10-04 11:53:04
【问题描述】:

我想在 Collection 视图的标题中添加一些按钮。从情节提要中启用“章节标题”。然后将“视图”拖到该标题。并将其背景颜色设为绿色

但我不知道为什么它不显示。如您所见,在模拟器中,标题只有一个空格。

【问题讨论】:

  • 您是否在源代码中声明了optional func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
  • @ezcoding 尝试添加,但给我错误
  • @GijoVarghese 实际的错误消息会有所帮助...

标签: ios swift swift2 uicollectionview swift3


【解决方案1】:

使用这个方法

CollectionHeaderCell中提供姓名

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath as IndexPath)

        headerView.backgroundColor = UIColor.blue;
        return headerView

    case UICollectionElementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath as IndexPath)

        footerView.backgroundColor = UIColor.green;
        return footerView

    default:

        assert(false, "Unexpected element kind")
    }
}

//为每个单元格索引路径制作一个单元格

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath)

      let profileimage: UIImageView = (cell.viewWithTag(5) as! UIImageView)

      profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!)
      return cell
 }

【讨论】:

  • 它完全正常工作我已经上传屏幕截图检查它。并比较你的代码和我的代码。
  • 我试过了。将所有语法转换为 swift 3。但它仍然在标题中显示一个空格
  • 有什么解决办法吗?
  • @MoazKhan 兄弟告诉我你有什么问题
  • 我的搜索栏在标题中不可见,经过一番搜索后我发现我需要设置标题大小并且它变得可见但主要问题是我想显示/隐藏它基于滚动我应该怎么做?
猜你喜欢
  • 2021-10-13
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多