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()

? ? {

? ? ? ? //? 設(shè)置 layOut

? ? ? ? let layout = UICollectionViewFlowLayout()

? ? ? ? layout.scrollDirection = UICollectionView.ScrollDirection.vertical? //滾動方向

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

? ? ? ? // 設(shè)置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

? ? ? ? // 備注我們的小標(biāo)題

? ? ? ? letindexString =String(indexPath.row)

? ? ? ? cell.myLabel.text="我的小標(biāo)題_"+ indexString

? ? ? ? // 獲取隨機(jī)顏色

//? ? ? ? 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="我的小標(biāo)題"

? ? ? ? label.textAlignment = .center

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

? ? ? ? returnlabel

? ? }()

? ? var myImageView:UIImageView = {

? ? ? ? letimageView =UIImageView()

? ? ? ? imageView.backgroundColor = .lightGray

? ? ? ? returnimageView

? ? }()


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容