swift UICollectionView

swift创建UICollectionView

  • CollectionVC
import UIKit

class CollectionVC: UIViewController {

    // 定义block
    var comBack:(()->())?
    private var listArray = [String]()
    private var collectionView : UICollectionView? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white

        // 加载数据
        addData()

        // 加载集合视图
        setupUI()
    }
}

extension CollectionVC {
    private func setupUI () {
        // 定义collectionView的布局类型
        let layout = UICollectionViewFlowLayout()
        // 设置cell的大小
        layout.itemSize = CGSize(width: view.bounds.width / 2, height: view.bounds.height / 3)
        // 设置滑动方向
        layout.scrollDirection = .vertical
        // 每个item之间最小间距
        layout.minimumInteritemSpacing = 0
        // 每行之间最小间距
        layout.minimumLineSpacing = 0

        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), collectionViewLayout: layout)
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.backgroundColor = UIColor.white
        view.addSubview(collectionView!)
        collectionView?.register(MainCollectionCell.self, forCellWithReuseIdentifier: "Identifier")
    }
}

extension CollectionVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let Identifier = "Identifier"
        let cell : MainCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier, for: indexPath) as! MainCollectionCell
        let dic = Dictionary<String, String>()
        cell.setValuesForKeys(dic)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
}



extension CollectionVC {
    private func addData () {
        addDatas { (array) in
            self.listArray += array
            self.collectionView?.reloadData()
        }
    }
}

extension CollectionVC {
    private func addDatas(compliteHander :@escaping(_ lists:[String])->()) {
        var listArray = [NSString]()
        DispatchQueue.global().async {
            for i in 0..<20  {
                listArray.append("\(i)" as NSString)
            }
            DispatchQueue.main.async {
                compliteHander(listArray as [String])
            }
        }
    }
}
  • MainCollectionCell
import UIKit

class MainCollectionCell: UICollectionViewCell {

    var  imageView : UIImageView?
    var titleLabel : UILabel?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension MainCollectionCell {
    private func setUpUI () {
        imageView = UIImageView()
        imageView?.frame = CGRect(x: 10, y: 10, width: 60, height: 60)
        self.contentView.addSubview(imageView!)
        titleLabel = UILabel()
        titleLabel?.frame = CGRect(x: 100, y: 30, width: 50, height: 30)
        titleLabel?.font = UIFont.systemFont(ofSize: 14)
        titleLabel?.textAlignment = .center
        titleLabel?.backgroundColor = UIColor.red
        self.contentView.addSubview(titleLabel!)
    }
}

extension MainCollectionCell {
    override func setValuesForKeys(_ keyedValues: [String : Any]) {
        titleLabel?.text = "fsdfsfsf"
        imageView?.image = UIImage(named: "head_2@3x")
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UICollectionView是一种用于展示多种类型的可滚动视图的类,它在Swift中也可使用。它是UIKit框架中的一部分,提供了一个灵活的方式来展示和管理大量的数据。 使用UICollectionView是为了展示具有多种布局和组件的数据集合。它类似于UITableView,但比UITableView更强大,可以支持多行或多列的自定义布局。 使用UICollectionView时,首先需要创建一个UICollectionViewLayout对象来定义布局。布局包含了单元格的大小、间距、滚动方向等信息。 然后,需要创建一个UICollectionView对象,并设置数据源和委托。数据源用于提供内容,委托用于处理用户交互和其他自定义操作。 接下来,实现UICollectionViewDataSource协议的方法,用于提供单元格的数量和内容。通常需要创建一个UICollectionViewCell的子类,并在数据源方法中使用该单元格。 然后,可以通过UICollectionViewDelegate协议的方法来响应选中单元格的操作,并执行相关的操作,如打开新的视图控制器或执行其他自定义操作。 最后,将UICollectionView添加到视图中,并通过reloadData()方法加载和刷新数据。 使用UICollectionView时,还可以自定义单元格的外观、动画效果和交互行为,以满足特定的设计要求。 在Swift中,可以使用UICollectionViewFlowLayout来快速创建简单的网格布局。同时,也可以使用自定义的布局来实现更复杂的布局需求,如瀑布流布局、层叠布局等。 总的来说,UICollectionView是一种强大而灵活的视图类,在Swift中可以方便地使用,用于展示和管理多种类型的数据集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值