iOS UICollectionView基础使用方法

来源:互联网 发布:3dmax软件培训 编辑:程序博客网 时间:2024/06/08 17:17

CollectionView 80%与tableView一样。很易学,更灵活

import UIKit

import Foundation


class ViewController:UIViewControllerUICollectionViewDelegateUICollectionViewDataSource{

       

    

//假设cell高度

    let collentionCellHeight:CGFloat =110


//屏幕宽度:

    let width =UIScreen.main.bounds.width

    

    var collection:UICollectionView!

    

//数据源

    var dataArr= [“1”,“23”,“4”,“21”,“2”,“32”,“5646”,“3234”,“435”,“242”]

       


    overridefunc viewDidLoad() {

        super.viewDidLoad()

        view.backgroundColor =UIColor.white

       

方法在下面

        initCollection()

        

    }

    

func initCollection(){

        

        //与tableView不同 一:必有layout

        let lay =UICollectionViewFlowLayout()

//每个元素大小:

        lay.itemSize =CGSize(width:width/3, height:collentionCellHeight)  

(一会有定义不同cell不同高度)

        lay.minimumInteritemSpacing =20//行间距

//构造collectionView        

        //collection

        collection =UICollectionView(frame:view.bounds,collectionViewLayout: lay)

//俩协议

collection.delegate =self

        collection.dataSource =self

        collection.backgroundColor = .white

//注册cell,和tableView一样

//用collectionView时候,cell都是自定义的,自定义的ELCollectionViewCell类在最下面。现在先直接写

    collection.register(ELCollectionViewCell.self,forCellWithReuseIdentifier:“cellID”)

 

        view.addSubview(collection)

        

    }

    

}   

      //  collectionView的代理方法,和tableView差不多

1.返回行数

    func collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section:Int) -> Int {

        returndataArr.count

    }

2. cell内容

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

        

        let cell:ELCollectionViewCell =collection.dequeueReusableCell(withReuseIdentifier:“cellID”, for: indexPath)as? ELCollectionViewCell       


        let model =dataArr[indexPath.item]

        cell.labelName?.text = “name:” + model

        cell.labelNum?.text =model

        

        return cell

    }

    

    func collectionView(_ collectionView:UICollectionView, didSelectItemAt indexPath:IndexPath) {


        //业务需求点击实现的东西自定 

    }

还有一个协议,是定义详细布局的,每个cell的高度之类的,叫 UICollectionViewDelegateFlowLayout,实现里面方法没什么好说的


}

class ELCollectionViewCell: UICollectionViewCell {

    

    let width =UIScreen.main.bounds.width

    

    var labelName:UILabel?

    var labelNum:UILabel?

    

    overrideinit(frame:CGRect) {

        super.init(frame: frame)

      

        backgroundColor =UIColor.lightgray

//加点外观

        layer.cornerRadius =15

        layer.borderWidth =0.3

        layer.borderColor =UIColor.green.cgColor

          

        labelName =UILabel(frame:CGRect(x:20, y:10, width:100, height:50))

        labelNum =UILabel(frame:CGRect(x:20, y: 60, width:100, height:50))

    

        labelName?.font =UIFont.boldSystemFont(ofSize:23)

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


        labelName?.textAlignment = .center

        labelNum?.textAlignment = .center

        addSubview(labelName!)

        addSubview(labelNum!)

    }

    

    requiredinit?(coder aDecoder:NSCoder) {

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

    }

    

}


0 0