将headerView从nib添加到UICollectionView swift

时间:2017-09-19 07:51:30

标签: swift uicollectionview

问题状态: 我需要为UICollectionView添加各种Header(如类别)。我可以实现这样的目标。

enter image description here

任何帮助将不胜感激。谢谢

3 个答案:

答案 0 :(得分:1)

假设您有XIB标签,并且您想要显示为集合视图的标题

首先在ViewDidLoad

中注册XIB

使用

open func register(_ viewClass: Swift.AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)

    collectionView.register(UINib.init(nibName: "NIB", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")

之后,您可以像这样设置分区集合视图

extension ViewController :UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 5;
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind.isEqual(UICollectionElementKindSectionHeader) {
            let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! MySectionHeaderClass
            cell.title = "MyString "
            return cell
        } else {
            return nil
        }
    }
}

//MAKE YOUR  Collection view item width to full screen

希望你能清楚

答案 1 :(得分:0)

之后,覆盖该函数确定indexPath.section并返回标题。

func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String!

而且,您必须将UIColectionView作为UITableViewCell的子视图, 所有UICollectionViewCell设置都将在UITableViewCell

中进行管理

答案 2 :(得分:0)

这对我有用。

 // in viewDidload

self.UICollectionView.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerId")

// use delegate delegate        
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath)
        var lblHeaher : UILabel!

    // as header is dequing that's why we need to check if label is there then no need to init label again and again
    if header.viewWithTag(101) == nil {
        lblHeaher = UILabel.init(frame: CGRect(x: 30, y: 0, width: servicesCollectionView.frame.width, height: 40))
        lblHeaher.tag = 101
    }else{
        lblHeaher = header.viewWithTag(101) as! UILabel
    }


    lblHeaher.text = "category \(indexPath.row)"
    lblHeaher.textAlignment = .left
    lblHeaher.textColor = UIColor.white
    lblHeaher.font = UIFont.init(name: "ProximaNova-Semibold", size: 20)!

    header.backgroundColor = .lightGray
    header.addSubview(lblHeaher)
    return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: self.frame.width, height: 40)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return numberofSection
}