Swift UICollectionView 实现

懒加载实现

lazy var collectView:UICollectionView = {
    let layout = UICollectionViewFlowLayout.init()
    layout.itemSize = CGSize(width: kScreen_width / 2 , height: kScreen_width / 2)
    layout.minimumLineSpacing=1;
    layout.minimumInteritemSpacing=1
    layout.footerReferenceSize = CGSize(width: screenWidth, height: 50)
    layout.headerReferenceSize = CGSize(width: screenWidth, height: 50)
    let collectView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
    collectView.backgroundColor = .groupTableViewBackground
    collectView.delegate = self
    collectView.dataSource = self
    collectView.showsVerticalScrollIndicator = true
    collectView.register(SwiftFooterCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "SwiftFooterCollectionReusableView")
    collectView.register(SwiftCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "SwiftCollectionViewCell")            collectView.register(SwiftHeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SwiftHeaderCollectionReusableView")
    return collectView
}()
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cellString = "SwiftCollectionViewCell"
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath)
    

    cell.backgroundColor = UIColor.gray
    return cell
} 
 //设置 区头和区尾 
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionView.elementKindSectionHeader  {
        let headerView:SwiftHeaderCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftHeaderCollectionReusableView", for: indexPath) as! SwiftHeaderCollectionReusableView
        
        return headerView
    }
    else
    {
        let footerView:SwiftFooterCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftFooterCollectionReusableView", for: indexPath) as! SwiftFooterCollectionReusableView
  return footerView
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容