swift UICollectionView

阎宝
2023-12-01

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")
    }
}
 类似资料:

相关阅读

相关文章

相关问答