创建一个简单的CollectionView

2019-08-17  本文已影响0人  午饭吃啥

遵循代理UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

class CollectionViewVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

因为要实现UICollectionViewDelegateFlowLayout代理方法,UICollectionViewDelegateFlowLayout继承于UICollectionViewDelegate,所以不添加UICollectionViewDelegate

懒加载一个CollectionView

lazy var collectionV : UICollectionView = {
        () -> UICollectionView in
    
        //collection
        let tempCollectionV = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), collectionViewLayout: UICollectionViewFlowLayout())
        //隐藏滑动条
        tempCollectionV.showsVerticalScrollIndicator = false
        //代理
        tempCollectionV.delegate = self
        tempCollectionV.dataSource = self
        //注册cell
        tempCollectionV.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "collectionCellID")
        
        return tempCollectionV
    }()

实现UICollectionViewDelegate

//多少组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    //每组多少个
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

实现 UICollectionViewDataSource

//创建cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let ID = "collectionCellID"
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath)
        let imageView:UIImageView? = (cell.viewWithTag(1) as? UIImageView)
        if imageView == nil{
            
            let imageV = UIImageView()
            imageV.backgroundColor = UIColor.orange
//            imageV.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH / 2 - 30, height: SCREEN_WIDTH / 2 - 30)
            imageV.tag = 1
            cell.backgroundColor = UIColor.orange
            cell.addSubview(imageV)
        }else{
            imageView?.backgroundColor = UIColor.orange
        }
        
        return cell
    }

实现 UICollectionViewDelegateFlowLayout

//cell大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: SCREEN_WIDTH / 2 - 15, height: SCREEN_WIDTH / 2 - 30)
    }
    //cell基础间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    //纵向最小间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        
        return 10
    }
    //横向最小间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        
        return 5
    }
上一篇下一篇

猜你喜欢

热点阅读