天天看点

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: )
        layout.footerReferenceSize = CGSize(width: kScreenWidth, height: )
        self.layout = layout

        // 布局
        layout.itemSize = CGSize(width: kScreenWidth / ,height: )
        let width = (kScreenWidth -  * ) / 
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = 
        layout.minimumLineSpacing = 
        layout.sectionInset = UIEdgeInsetsMake(, , , )
        let rect = CGRect(x: , y: , width: kScreenWidth, height: CGFloat(kScreenHeight) - CGFloat(kStatusBarHeight) - )
        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 ==  {
                (reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[indexPath.section]
                (reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[indexPath.section]
            } else {
                (reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[singleShowItemIndex - ]
                (reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[singleShowItemIndex - ]
            }
        }
        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 -  {
            return CGSize(width: kScreenWidth, height: )
        } else {
            return CGSize(width: kScreenWidth, height: )
        }
    }
           
时间不够,只能这么帖代码了,以后有时间了来完善这些内容,抱歉