【问题标题】:How do I make my CollectionView Cells filter data from my TableView?如何让我的 CollectionView 单元格从我的 TableView 中过滤数据?
【发布时间】:2020-01-29 17:19:48
【问题描述】:

我在尝试让我的集合视图为集合视图中设置的类别过滤表视图中的数据时遇到问题。我的目标是在集合视图中选择一个类别,从 categoryLabel 中显示的类别中显示 tableview 中所选类别的搜索结果:

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]
      }
  }

【问题讨论】:

  • 你有什么问题?
  • 你有没有尝试在collectionview单元格上选择方法?
  • collectionview 单元格的 select 方法在 UISearchBarDelegate 的 textdichange 中使用了相同的代码
  • 这应该像在您的集合视图上使用didDeselectItemAtIndexPath 委托函数一样简单,获取类别标识符,并相应地过滤(或获取)您的产品数组。我不完全确定我明白你在问什么,但这就是我收集到的。
  • @Koen 我正在尝试使用 collectionview 在类别的 tableview 中显示搜索结果

标签: ios swift uitableview uicollectionview google-cloud-firestore


【解决方案1】:

据我了解您的问题,

首先,这是我的建议:

  • 不要使用 UITableView 和 UICollectionView。无需同时使用两个 UI 滚动类。你可以只使用 UICollectionView 来实现它。
  • 只需为类别过滤器列表创建一个 collectionView 单元格并在第 0 个索引处返回它。

现在来解决您的问题,您可以简单地使用 CollectionView 中的部分数量来显示具有每个过滤类别的产品。

【讨论】:

  • 这 2 条建议是好主意,但我该怎么做 “现在来解决您的问题,您可以使用 CollectionView 中的部分数量来显示具有每个过滤类别的产品。” 这样我就可以让它工作了
  • 您可以在每个部分使用 UICollectionReusableView 类在 UICollectionView 标题中显示类别标签。
  • 好的,我已经做到了,但是我将如何委派标签以便在选择时能够搜索特定类别
  • 现在您必须在集合标题上显示选定的类别。
  • 好的,我该怎么做.. 我会使用 didSelectRowAtIndexPath 什么的吗?我只是对你在说什么感到困惑
猜你喜欢
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多