首页 > 解决方案 > 在 UICollectionView 中动态插入 Item

问题描述

我正在处理需要UICollectionView动态添加项目的要求。

这是我的代码ViewController

import UIKit

class ViewController: UIViewController {
    enum Direction {
        case Horizonatal
        case Verticle
    }

    var enumDirection: Direction = .Verticle
    var direction = "Verticle"
    var SectionsAndRows = [Int]()

    override func viewDidLoad() {
        super.viewDidLoad()
        SectionsAndRows.append(4)
        SectionsAndRows.append(3)
        SectionsAndRows.append(2)
        SectionsAndRows.append(1)
        //SectionsAndRows.append(5)
    }

    @IBOutlet var gridCollectionView: UICollectionView! {
        didSet {
            gridCollectionView.bounces = false
        }
    }

    @IBOutlet var gridLayout: UICollectionViewFlowLayout! {
        didSet {
            //gridLayout.stickyRowsCount = 0
            gridLayout.scrollDirection = .horizontal
            //gridLayout.stickyColumnsCount = 0
            gridLayout.minimumLineSpacing = 5
            gridLayout.minimumInteritemSpacing = 5
        }
    }
 }

// MARK: - Collection view data source and delegate methods

extension ViewController: UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return SectionsAndRows.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(SectionsAndRows[section])
        return SectionsAndRows[section]
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.reuseID, for: indexPath) as? CollectionViewCell else {
            return UICollectionViewCell()
        }
        print("Current Section ==\(indexPath.section) CurrentRow ===\(indexPath.row) and its rows count ==\(SectionsAndRows[indexPath.section])")

        cell.titleLabel.text = ""
        cell.btn.addTarget(self, action: #selector(handleAdd(sender:)), for: .touchUpInside)
        cell.btn.tag = (indexPath.section * 1000) + indexPath.row
        if enumDirection == .Verticle {
            if indexPath.section == SectionsAndRows.count - 1 {
                cell.btn.setTitle("+", for: .normal)
            } else {
                cell.btn.setTitle("\(indexPath)", for: .normal)
            }

        }
        return cell
    }

    @objc func handleAdd(sender: UIButton) {
        // Perform some opration
    }
}

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}

CollectionViewCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell {
    static let reuseID = "CollectionViewCell"

    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var titleLabel: UILabel!
}

如果您运行代码,它将在每行中显示一个包含 1 行和 1 列的集合。如果您取消注释viewDidLoad()( SectionsAndRows.append(5)) 函数的最后一行,则它可以正常工作。

我的观察是,最后一部分的CollectionView列数最多。这是正确的还是 CollectionView 的错误?

标签: iosswiftuicollectionviewuicollectionviewlayoutuicollectionviewflowlayout

解决方案


推荐阅读