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
    }
    
}

注册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")

自定义 分区头部,尾部代理

    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 方法特性

##水平滑动
layout.scrollDirection = .horizontal

错误


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容