gpt4 book ai didi

ios - 根据firebase逻辑,在uicollectionview中快速分段控制显示/隐藏单元格

转载 作者:行者123 更新时间:2023-12-01 22:00:30 25 4
gpt4 key购买 nike

我很难根据 FB 实时数据库中的项目子节点来显示/隐藏我的 collectionview 单元格。该问题由三部分组成:FB 数据库、一个collectionview 和一个分段控件。我的目标是在 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

我还有一个自定义产品类来在他们的单元格中显示我的项目:
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。此外,我不确定在我的数据库中存储逻辑部分的最佳方式是什么。

最佳答案

扩展您的产品类:

class Product {
...
let logic: [String]?

init(...) {
...
logic = rawData["logic"] as? [String]
}
}

在您的 CollectionViewDataSource 添加一些变量来存储当前状态
var products: [Products]
var filteredProducts: [Product]
var currentFilter: String? {
didSet {
guard let currentFilter = currentFilter else {
filteredProducts = products
return
}
filteredProducts = products.filter { product in
return product.logic.contains(currentFilter)
}
}
}

扩展你的 handleSegment 函数:
@objc fileprivate func handleSegment() {
print(segmentController.selectedSegmentIndex)
currentFilter = segmentController.titleForSegment(at: segmentController.selectedSegmentIndex)
collectionView.reloadData()
}

在您的collectionView 数据源中,使用filteredProducts 来构建单元格。

关于ios - 根据firebase逻辑,在uicollectionview中快速分段控制显示/隐藏单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60152072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com