【问题标题】:How to add header and footer view of UICollectionView in Swift如何在 Swift 中添加 UICollectionView 的页眉和页脚视图
【发布时间】:2015-12-15 05:59:02
【问题描述】:

我在控制器类中添加了委托函数,但它不会在运行时调用。

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}

【问题讨论】:

标签: ios swift uicollectionview


【解决方案1】:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView

        headerView.backgroundColor = UIColor.blueColor();
        return headerView

    case UICollectionElementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView

        footerView.backgroundColor = UIColor.greenColor();
        return footerView

    default:

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

【讨论】:

  • 这个委托在运行时没有调用
  • 我打电话给这个:(Swift 3)collectionView?.register(HeaderCollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell") 我仍然无法打电话给代表!
【解决方案2】:

我遵循本教程Appcoda tutorial 并更新代码为 swift 4.0

注册课程:

self.collectionView.register(HeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeadView")

代表:

// For header size (required)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:50.0)
}


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

    switch kind {

    case UICollectionElementKindSectionHeader:

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

        headerView.backgroundColor = UIColor.blue;
        return headerView

    default:

        fatalError("Unexpected element kind")
    }
}

【讨论】:

    【解决方案3】:

    没有调用Delegate方法,因为需要为补充视图注册一个类;

    yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionHeaderID")
    
    yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "collectionFooterID")
    

    【讨论】:

      【解决方案4】:

      如果你使用自定义 UICollectionViewFlowLayout

      设置 headerReferenceSize

      let flowLayout = UICollectionViewFlowLayout()
          flowLayout.scrollDirection = .vertical
          flowLayout.minimumLineSpacing = 0
          flowLayout.minimumInteritemSpacing = 0
          flowLayout.sectionHeadersPinToVisibleBounds = true
          flowLayout.headerReferenceSize = CGSize(width:      self.collectionView.frame.size.width, height: 50)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-23
        • 2013-05-07
        • 2014-10-01
        • 2013-09-05
        • 2012-01-25
        • 2014-05-29
        • 2011-11-27
        相关资源
        最近更新 更多