iOS UICollectionView 详解

importUIKit

////

////  ViewController.swift

////  test

////

////  Created by Mac on 2023/5/30.

////

//

importUIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        letcell = collectionView.dequeueReusableCell(withReuseIdentifier:idenContentString,for: indexPath)as!MyTestCollectionViewCell

        returncell

    }

    let screenWidth = UIScreen.main.bounds.width

    let screenHeight = UIScreen.main.bounds.height

    let idenContentString = "idenContentString"

    let headIdenString = "headIdenString"

    override func viewDidLoad() {

        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = true

        self .makeUICollectionView()

    }

    func makeUICollectionView()

    {

        //  设置 layOut

        let layout = UICollectionViewFlowLayout()

        layout.scrollDirection = UICollectionView.ScrollDirection.vertical  //滚动方向

        layout.itemSize=CGSizeMake((screenWidth-30)/2,80)

        // 设置CollectionView

        letourCollectionView :UICollectionView=UICollectionView(frame:CGRectMake(0,0,screenWidth,screenHeight),collectionViewLayout: layout)

        ourCollectionView.delegate=self

        ourCollectionView.dataSource=self

        ourCollectionView.backgroundColor= .white

        ourCollectionView .register(MyTestCollectionViewCell.self,forCellWithReuseIdentifier:idenContentString)

        self.view.addSubview(ourCollectionView)

    }

    //MARK: UICollectionViewDataSource

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return1

    }

    funccollectionView(_collectionView:UICollectionView,numberOfItemsInSectionsection:Int) ->Int{

        return60

    }

    func  collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        letcell  = collectionView.dequeueReusableCell(withReuseIdentifier:idenContentString,for: indexPathasIndexPath)as!MyTestCollectionViewCell

        // 备注我们的小标题

        letindexString =String(indexPath.row)

        cell.myLabel.text="我的小标题_"+ indexString

        // 获取随机颜色

//        let colorValue1 :CGFloat!  = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))

//        let colorValue2 :CGFloat!  = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))

//        let colorValue3 :CGFloat!  = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))

        cell.myImageView.backgroundColor= .red

        returncell;

    }

    //MARK:UICollectionViewDelegate

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        print("tap ==\(indexPath.row)")

    }

    //MARK:UICollectionViewDelegateFlowLayout

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{

        returnUIEdgeInsets(top:5,left:10,bottom:5,right:10)

    }

}

class MyTestCollectionViewCell: UICollectionViewCell {

    override init(frame: CGRect) {

        super.init(frame: frame)

        // 由于测试,先不考虑 自动布局 后期可以考虑 SnapKit

        self.myLabel.frame=CGRectMake(10,60, frame.size.width-20,20)

        self.myImageView.frame=CGRectMake(10,0, frame.size.width-20,60)

        self.contentView.backgroundColor = UIColor(red: 230/255.0, green: 230/255.0, blue: 230/255.0, alpha: 1.0)

        self.contentView .addSubview(self.myLabel)

        self.contentView .addSubview(self.myImageView)

    }

    requiredinit?(coderaDecoder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    varmyLabel:UILabel= {

        letlabel =UILabel()

        label.text="我的小标题"

        label.textAlignment = .center

        label.font=UIFont.systemFont(ofSize:12)

        returnlabel

    }()

    var myImageView:UIImageView = {

        letimageView =UIImageView()

        imageView.backgroundColor = .lightGray

        returnimageView

    }()


}

你可能感兴趣的:(iOS UICollectionView 详解)