如何以编程方式在 Swift 中设置动态数量的 collection 视图

How to programmatically set up a dynamic number of collection views in Swift

我有很多Decks的列表,主要有一个name和一个Cards的列表。下面显示了 class 结构。

class Deck{
    var id:String
    var name:String
    var cards:[Card]

    init(id:String, name:String, cards:[Card]) {
        self.id = id
        self.name = name
        self.cards = cards
    }
}

class Card {
    var id:String
    var name:String

    init(id:String, name:String){
        self.id = id
        self.name = name
    }
}

我需要能够为每个牌组创建一个 UICollectionView,其中每个单元格应代表一个带有单个卡片名称的按钮,并且我需要能够识别哪个单元格的按钮被点击。这意味着我需要允许 UICollectionView(套牌列表)的动态数量和 UICollectionViewCell(卡片列表)的动态数量。请注意,我已经了解如何动态创建单元格并确定点击了哪个单元格的按钮。

总的来说,主要问题在于我不知道有多少套牌,因此我不知道我必须创建多少 UICollectionView。但是,由于所有 UICollectionViews 的结构都相同但填充了不同的值(即单元格数、单元格的标签标题具有不同的值),我想知道动态创建 UICollectionViews 的方法是什么(NOT UICollectionViewCells) 并用 decks:[Deck]

列表中的套牌填充它们

非常感谢!底部的图片大致显示了我想做的事情。

您已经有了一个解决方案,您有一个列表,其中有一个项目,因此您必须动态实施,因此您只需要计算列表中的项目数量。

所以你在 func 中使用 items.count:-

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Your Cell Identifier Name", for: indexPath)
    // This will return a each item name from the item list and display in to a cell
    cell.textLabel.text = item[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // Here you write a code when user select a particular cell
}

这是适合您的简单解决方案。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //number of items in Decks Array will be its count
    if collectionView == dockCollectionView {
        return Decks.count  //It will tell the collection view how much cell will be there
    }
    if collectionView == cardsCollectionView {
        return Cards.count
    }
}

//Setup your resuable cell here
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    //Setup for Dock CollectionView
    if collectionView == dockCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Your Dock Cell Identifier Name", for: indexPath)
        // This will return a each item name from the item list and display in to a cell
        cell.textLabel.text = Decks[indexPath.row].name
        return cell
    }
    //Setup for Cards CollectionView
    if collectionView == cardsCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Your Cards Cell Identifier Name", for: indexPath)
        // This will return a each item name from the item list and display in to a cell
        cell.textLabel.text = Cards[indexPath.row].name
        return cell
    }
}

//Now get to know which cell is tapped
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == dockCollectionView {
        print(Decks[indexPath.row].name)
    }
    if collectionView == cardsCollectionView {
        print(Cards[indexPath.row].name)
    }
    // Here you write a code when user select a particular cell
}

目前还不清楚您是否真的需要 UICollectionView 对象的集合,或者您可以在同一个集合视图中处理多个部分。无论如何,如果您确实喜欢多个集合视图,只需创建一个集合或 table 视图来显示牌组列表并将牌组的集合视图放入单元格中。

class DeckCell: UICollectionViewCell { // or class DeckCell: UITableViewCell
    var deck: Deck = .empty

    @IBOutlet weak var collectionView: UICollectionView!
}

extension DeckCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.deck.cards.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath)
        cell.card = self.deck.cards[indexPath.row]
        return cell
    }
}