Swift 分段控制 show/hide uicollectionview 中的单元格取决于 firebase 逻辑
Swift segmented contorl show/hide cells in uicollectionview depending on firebase logic
我很难根据 FB 实时数据库中的项目子节点尝试 display/hide 我的 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
我还有一个自定义产品 class 可以在它们的单元格中显示我的项目:
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 函数,我能够打印出已选择的段。但这就是问题发生的地方。我已经尝试创建新数组来拆分项目,以便项目根据它们的 "logic" 子节点位于数组中。但是我无法制作产品类型的数组,因此我无法使用它们来重新填充集合视图。我也不确定在我的数据库中存储逻辑部分的最佳方式是什么。
扩展您的产品class:
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 构建单元格。
我很难根据 FB 实时数据库中的项目子节点尝试 display/hide 我的 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
我还有一个自定义产品 class 可以在它们的单元格中显示我的项目:
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 函数,我能够打印出已选择的段。但这就是问题发生的地方。我已经尝试创建新数组来拆分项目,以便项目根据它们的 "logic" 子节点位于数组中。但是我无法制作产品类型的数组,因此我无法使用它们来重新填充集合视图。我也不确定在我的数据库中存储逻辑部分的最佳方式是什么。
扩展您的产品class:
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 构建单元格。