swift中UICollectionView使用

自定义cell

import UIKit

class CollectionViewCell: UICollectionViewCell {
    
    var imgView: UIImageView!
    var nameLabel: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.createCellUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createCellUI()  {
        
        self.imgView = UIImageView.init(frame: CGRect(x: 10, y: 10, width: self.contentView.frame.size.width - 20 , height: self.contentView.frame.size.width - 20))
        self.imgView.backgroundColor = .lightGray
        self.contentView.addSubview(self.imgView)
        
        self.nameLabel = UILabel.init(frame: CGRect(x: 10, y: self.contentView.frame.size.width - 20 + 10, width: self.contentView.frame.size.width - 20, height: 30))
        self.nameLabel.text = "名称"
        self.nameLabel.textAlignment = .center
        self.contentView.addSubview(self.nameLabel)
    }

}

 

创建collectionView

import UIKit

class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    let cell_identifier:String = "collectionCell"
    let cell_head:String = "collectionHeadCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.view.addSubview(self.collectionView)
    }
    
    //分区数
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    
    //每个分区含有的 item 个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    //每个cell中的内容
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cell_identifier, for: indexPath)
        
        return cell
    }
    
    //返回区头、区尾
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        var headview = CollectionHeaderView();
        if kind == UICollectionView.elementKindSectionHeader {
            headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: cell_head, for: indexPath as IndexPath) as! CollectionHeaderView;
            
            headview.label.text = "表头"
        }
        
        return headview;
        
    }
    
    //每个分区的内边距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
    }
    
    //最小 item 间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

           return 0;
    }
    
    //最小行间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

        return 0;
    }
    
    //item 的尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: self.view.frame.size.width / 2, height: self.view.frame.size.width / 2 + 30)
    }
    
    //每个分区区头尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

        return CGSize (width: self.view.frame.size.width, height: 50)

    }
    
    //MARK: - 懒加载
    lazy var collectionView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        
        let collectionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 64), collectionViewLayout: layout)
        collectionView.backgroundColor =  .systemGroupedBackground
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: cell_identifier)
        collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: cell_head)
        
        return collectionView
    }()

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值