美文网首页Swift
Swift UICollectionView 实现

Swift UICollectionView 实现

作者: 江河_ios | 来源:发表于2020-06-15 13:38 被阅读0次

懒加载实现

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

相关文章

网友评论

    本文标题:Swift UICollectionView 实现

    本文链接:https://www.haomeiwen.com/subject/cxpcxktx.html