uicollectionview中的快速分段控制显示/隐藏单元,具体取决于firebase逻辑

问题描述 投票:1回答:1

我很难根据FB实时数据库中的子节点项显示/隐藏我的collectionview单元。该问题包括三个部分:FB数据库,集合视图和分段控件。我的目标是在collectionview中显示不同的单元格,具体取决于该项是否具有带有特定字符串值的子项。

我的数据库如下:

Items
   category1
      item1
         name: item1
         imageUrl: item1Url
         logic
            one
            three
      item2
         name: item1
         imageUrl: item1Url
         logic
            two
            three
   category2
      item1
         name: item1
         imageUrl: item1Url
         logic
            two
            four
      item2
         name: item1
         imageUrl: item1Url
         logic
            one
            two

我还有一个自定义的Product类,可在其单元格中显示我的商品:

class Product {
    var category: String?
    var name: String?
    var imageUrl: String?

    init(rawData: [String: AnyObject]) {
        name = rawData["name"] as? String
        imageUrl = rawData["imageUrl"] as? String
        category = rawData["category"] as? String
    }
} 

我使用此功能从Firebase数据库加载我的物品:

func loadCategoryName() {
    ref = Database.database().reference().child("Items").child(selectedCategoryFromPreviousVC)
    ref.observeSingleEvent(of: .value) { (snapshot) in
        if let data = snapshot.value as? [String: AnyObject] {
            self.itemArray = []
            let rawValues = Array(data.values)
            for item in rawValues {
                let product = Product(rawData: item as! [String: AnyObject])
                product.category = self.selectedCategoryFromPreviousVC
                self.itemArray.append(product)
            }

            // Sort item array by rating; if rating is same, sort by name
            self.itemArray.sort { (s1, s2) -> Bool in
                if s1.rating == s2.rating {
                    return s1.name! < s2.name!
                } else {
                    return s1.rating > s2.rating
                }
            }
            self.collectionView?.reloadData()
        }
    }
}

我的itemArray现在包含我所有的商品作为自定义产品,我可以在其单元格中显示它们。

我的分段控件:] >>

func someFunc() {
    let segmentController = UISegmentedControl(items: ["one", "two", "three", "four"])

    segmentController.selectedSegmentIndex = 0
    self.navigationItem.titleView = segmentController
    segmentController.addTarget(self, action: #selector(handleSegment), for: .valueChanged)
}

@objc fileprivate func handleSegment() {
    print(segmentController.selectedSegmentIndex)
}

使用handleSegment函数,我可以打印出已选择的段。但这是发生问题的地方。我尝试创建新的数组来拆分项目,以便项目根据其“逻辑”子节点位于数组中。但是,我无法制作Product类型的数组,因此我可以使用它们重新填充collectionview。同样,我也不十分确定将逻辑部分存储在数据库中的最佳方法是什么。

我很难根据FB实时数据库中的子节点项显示/隐藏我的collectionview单元。该问题包括三个部分:FB数据库,collectionview和...

ios swift firebase-realtime-database uicollectionview uisegmentedcontrol
1个回答
1
投票

扩展您的产品类别:

© www.soinside.com 2019 - 2024. All rights reserved.