我在尝试获取我的集合视图以过滤表视图中针对集合视图中设置的类别的数据时遇到问题。我的目标是要在集合视图中选择一个类别,以便在tableview中显示来自categoryLabel中显示的类别的所选类别的搜索结果:
tableview已经连接到搜索栏,并准确显示搜索结果。但是我希望它对集合视图中的选择/类别执行相同的操作,以过滤出选择的结果,以在集合视图中显示该特定类别的搜索结果。
我的数据存储在Cloud Firestore中
import Foundation
import UIKit
class Category {
var categoryLabel: String
init(categoryLabel: String) {
self.categoryLabel = categoryLabel
}
class func createCategoryArray() -> [Category] {
var categorys: [Category] = []
let category1 = Category(categoryLabel: "All")
let category2 = Category(categoryLabel: "Flower")
let category3 = Category(categoryLabel: "CBD")
let category4 = Category(categoryLabel: "Pre-Roll")
let category5 = Category(categoryLabel: "Pens")
let category6 = Category(categoryLabel: "Cartridges")
let category7 = Category(categoryLabel: "Concentrate")
let category8 = Category(categoryLabel: "Edible")
let category9 = Category(categoryLabel: "Drinks")
let category10 = Category(categoryLabel: "Tinctures")
let category11 = Category(categoryLabel: "Topical")
let category12 = Category(categoryLabel: "Gear")
categorys.append(category1)
categorys.append(category2)
categorys.append(category3)
categorys.append(category4)
categorys.append(category5)
categorys.append(category6)
categorys.append(category7)
categorys.append(category8)
categorys.append(category9)
categorys.append(category10)
categorys.append(category11)
categorys.append(category12)
return categorys
}
}
import UIKit
class CategoryScrollCell: UICollectionViewCell {
@IBOutlet weak var categoryScroll: UILabel!
@IBOutlet weak var view: UIView!
func setCategory(category: Category) {
categoryScroll.text = category.categoryLabel
}
}
import UIKit
import Firebase
class ProductListController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var productListCollectionView: UICollectionView!
@IBOutlet weak var productListTableView: UITableView!
var categorys: [Category] = []
var searchActive : Bool = false
var productInventory: [ProductList] = []
var productSetup: [ProductList] = []
override func viewDidLoad() {
super.viewDidLoad()
categorys = Category.createCategoryArray()
productListCollectionView.dataSource = self
productListCollectionView.delegate = self
productListTableView.dataSource = self
productListTableView.delegate = self
searchBar.delegate = self
fetchProducts { (products) in
self.productSetup = products
self.productListTableView.reloadData()
}
}
func fetchProducts(_ completion: @escaping ([ProductList]) -> Void) {
let ref = Firestore.firestore().collection("products")
ref.addSnapshotListener { (snapshot, error) in
guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
return
}
completion(snapshot.documents.compactMap( {ProductList(dictionary: $0.data())} ))
}
}
}
extension ProductListController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productSetup.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProductListCell") as?
ProductListCell else { return UITableViewCell() }
cell.configure(withProduct: productSetup[indexPath.row])
return cell
}
}
extension ProductListController: UICollectionViewDelegate, UICollectionViewDataSource{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categorys.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryScrollCell", for: indexPath) as! CategoryScrollCell
let category = categorys[indexPath.row]
cell.setCategory(category: category)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("selected")
self.productSetup.category = self.productInventory[indexPath.row]
}
}
答案 0 :(得分:0)
据我了解,您的问题
首先,这是我的建议:
现在要解决您的问题,只需使用CollectionView中的部分数量即可显示具有每个过滤类别的产品。