> 文章列表 > iOS swift UICollectionView

iOS swift UICollectionView

iOS swift UICollectionView

文章目录

  • 1.纯代码自定义UICollectionViewCell
  • 2.禁止滑动(弹簧效果)
  • 3.UICollectionView的长按拖动
  • 2.在一个控制器中放两个UICollectionView或者UITableView,代理方法要怎么写

1.纯代码自定义UICollectionViewCell

import UIKitclass NewDeviceBottomCollectionViewCell: UICollectionViewCell {var model:NewBottomModel = NewBottomModel(image: "海拔", text: "test") {didSet{reloadData()}}override init(frame: CGRect) {super.init(frame: frame)setupUI()}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}var bottomLabel = UILabel()var topImageView = UIImageView()func setupUI(){contentView.backgroundColor = newUIBlackcontentView.addSubview(topImageView)topImageView.snp.makeConstraints { make inmake.top.equalToSuperview().offset(10)make.width.height.equalTo(25)make.centerX.equalToSuperview()}contentView.addSubview(bottomLabel)bottomLabel.textColor = .whitebottomLabel.snp.makeConstraints { make inmake.centerX.equalToSuperview()make.top.equalTo(topImageView.snp_bottom).offset(10)}}func reloadData(){bottomLabel.text = "平均速度"topImageView.image = UIImage(named: model.image)}
}

参考博客:
Swift之自定义UICollectionViewCell - csdn

2.禁止滑动(弹簧效果)

        collectionView.bounces = false

参考博客:
iOS swift-UIScrollview,UITableview,UICollectionView单独禁止下拉(上拉)

3.UICollectionView的长按拖动

参考博客:
Swift下使用UICollectionView 实现长按拖拽功能

2.在一个控制器中放两个UICollectionView或者UITableView,代理方法要怎么写