iOS collectionView添加头部底部view

定义一个collectionview
/// 创建colloectionview
    private func createCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = UICollectionViewScrollDirection.vertical
        // 以下两行非常重要 必须设置headerReferenceSize或者footerReferenceSize的大小才能征程的显示header和footer
        layout.headerReferenceSize = CGSize(width: kScreenWidth, height: 45)
        layout.footerReferenceSize = CGSize(width: kScreenWidth, height: 50)
        self.layout = layout

        // 布局
        layout.itemSize = CGSize(width: kScreenWidth / 5,height: 80)
        let width = (kScreenWidth - 3 * 80) / 4
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 10
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        let rect = CGRect(x: 0, y: 0, width: kScreenWidth, height: CGFloat(kScreenHeight) - CGFloat(kStatusBarHeight) - 44)
        collectionView = UICollectionView.init(frame: rect, collectionViewLayout: layout)
        collectionView?.delegate = self as UICollectionViewDelegate
        collectionView?.dataSource = self as UICollectionViewDataSource;
        //注册一个cell
        let nib = UINib(nibName: "SCTHomeNormalCollectioncell", bundle: nil)
        collectionView?.register(nib, forCellWithReuseIdentifier: "SCTHomeNormalCollectioncell")

        // register a footer nib
        let nibFooter = UINib(nibName: "SCTCollectionReusebleFooterView", bundle: nil)
//        collectionView?.register(nibFooter, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SCTCollectionReusebleFooterView")
//        collectionView?.register(SCTCollectionReusableFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SCTCollectionReusableFooterView")
        collectionView?.register(nibFooter, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "UICollectionElementKindSectionFooter")

        // register a header nib
        let nibHeader = UINib(nibName: "SCTCollectionReusableHeaderView", bundle: nil)
//        collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SCTCollectionReusableHeaderView")

//        collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SCTCollectionReusableHeaderView")
        collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UICollectionElementKindSectionHeader")

        collectionView?.backgroundColor = UIColor.clear
        self.view.addSubview(collectionView!)
    }
实现协议
UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
collectionView的协议中实现header和footer
 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:UICollectionReusableView!

        if kind == UICollectionElementKindSectionHeader
        {
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionElementKindSectionHeader", for: indexPath) as! SCTCollectionReusableHeaderView
            if singleShowItemIndex == 0 {
                (reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[indexPath.section]
                (reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[indexPath.section]
            } else {
                (reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[singleShowItemIndex - 1]
                (reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[singleShowItemIndex - 1]
            }
        }
        else if kind == UICollectionElementKindSectionFooter
        {
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionElementKindSectionFooter", for: indexPath) as! SCTCollectionReusebleFooterView
        }
//        print("\(kind)--\(indexPath.section)---\(smartSectionTitles.count - 1)")
        return reusableview
    }
具体定义每个footer或者header的size

举例为footer

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {

        if section == smartSectionTitles.count - 1 {
            return CGSize(width: kScreenWidth, height: 60)
        } else {
            return CGSize(width: kScreenWidth, height: 0)
        }
    }

时间不够,只能这么帖代码了,以后有时间了来完善这些内容,抱歉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值