Ios 如何使CollectionView单元格从TableView中筛选数据?
我在尝试让我的集合视图为集合视图中设置的类别筛选tableview中的数据时遇到问题。我的目标是在“集合”视图中选择一个类别在“表格视图”中显示所选类别的搜索结果,该表格视图显示了categoryLabel中显示的类别: tableview已连接到搜索栏并准确显示搜索结果。但我希望它对集合视图中的选择/类别执行相同的操作,以过滤掉所选结果,从而在集合视图中显示该特定类别的搜索结果 我的数据存储在云Firestore中Ios 如何使CollectionView单元格从TableView中筛选数据?,ios,swift,uitableview,uicollectionview,google-cloud-firestore,Ios,Swift,Uitableview,Uicollectionview,Google Cloud Firestore,我在尝试让我的集合视图为集合视图中设置的类别筛选tableview中的数据时遇到问题。我的目标是在“集合”视图中选择一个类别在“表格视图”中显示所选类别的搜索结果,该表格视图显示了categoryLabel中显示的类别: tableview已连接到搜索栏并准确显示搜索结果。但我希望它对集合视图中的选择/类别执行相同的操作,以过滤掉所选结果,从而在集合视图中显示该特定类别的搜索结果 我的数据存储在云Firestore中 import Foundation import UIKit c
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
}
}
我理解你的问题 首先,我的建议是:
- 不要使用UITableView和UICollectionView。不需要同时使用两个UI滚动类。您只能使用UICollectionView来实现它
- 只需为类别筛选器列表创建一个collectionView单元格,并在第0个索引中返回它
现在来谈谈你的问题,您可以简单地使用CollectionView中的节数来显示每个筛选类别的产品。您的问题是什么?您是否尝试在CollectionView单元格中选择方法?CollectionView单元格中选择方法使用与UISearchBardelegate的textdichange中相同的代码这应该与使用
didDecelesitematIndexPath
委托集合视图上的函数,获取类别标识符,并相应地筛选(或获取)产品数组。我不完全确定我是否理解你的要求,但这就是我收集的信息。@Koen我正在尝试使用collectionview在tableview中显示类别的搜索结果。这两个建议很好,但我该怎么做呢“现在来谈谈您的问题,您只需使用CollectionView中的节数即可显示每个筛选类别的产品。“为了使其有效,您可以在UICollectionView标头中使用UICollectionReusableView类在每个节中显示类别标签。好的,我已经这样做了,但是我如何委派标签才能在选择时搜索特定类别现在您必须在集合标头上显示所选类别。好的,我该怎么做…”。。我会使用didSelectRowAtIndexPath还是什么?我只是有点不明白你在说什么
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]
}
}