UICollectionView

初始化

lazy var clvData: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        layout.minimumInteritemSpacing = 0; //列與列之間的間距
        layout.minimumLineSpacing = 0;//行與行之間的間距
        layout.itemSize = CGSize.init(width: kSCREEN_WIDTH/3, height: 100)
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        let cell = UINib.init(nibName: "KSampleClvCell", bundle: nil)
        collectionView.register(cell, forCellWithReuseIdentifier: "KSampleClvCell")
        return collectionView
    }()

self.view.addSubview(clvData)
        clvData.snp.makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }

常用代理方法

extension WorkHomeVC: UICollectionViewDelegate, UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout {
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KSampleClvCell", for: indexPath) as! KSampleClvCell
        
        return cell
    }
    
}

注冊(cè)cell,SectionHead,SectionFoot

#IB 類型
let secionHead = UINib.init(nibName: "MMSectionHeadView", bundle: nil)
collectionView.register(secionHead, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView")
collectionView.register(secionHead, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionHeadView")

#class 類型
  collectionView.register(MMCollectCell.self, forCellWithReuseIdentifier:"MMCollectCell")
  collectionView.register(MMSectionHeadView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView")
  collectionView.register(MMSectionFootView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionFootView")

自定義 分區(qū)頭部,尾部代理

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
    {
        var v = UICollectionReusableView()
        if kind == UICollectionElementKindSectionHeader
        {
            v = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView", for: indexPath)as! MMSectionHeadView
        }
        else if kind == UICollectionElementKindSectionFooter
        {
             v = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionFootView", for: indexPath)as! MMSectionFootView
        }
        return v
    }
    /* sectionHeadView 尺寸*/
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
        
        return CGSize(width: UIScreen.main.bounds.width, height: 50)
    }
    
    /* sectionFootView 尺寸*/
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
        
        return CGSize(width: UIScreen.main.bounds.width, height: 50)
    }

item Size 代理

 /* item 尺寸*/
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: UIScreen.main.bounds.width/2, height: 100)
    }

collectionView 方法特性

##水平滑動(dòng)
layout.scrollDirection = .horizontal

錯(cuò)誤


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容