我一直试图通过tableview上方的collection视图对我的搜索结果建立索引,以便当我在collection view中按下一个单元格时,它可以为表中的结果编制索引,如下图所示
到目前为止,代码的工作程度是,一旦我在集合视图中按一个单元格,它就会将所选类别的文本放置在搜索栏中,并获得搜索结果,但是片刻之后模拟器崩溃了在表视图的cellForRowAt中的cell.configure(withProduct: itemInventory[indexPath.row])
上出现错误线程1:致命错误:索引超出范围
感谢您的帮助
import UIKit import Firebase import FirebaseFirestore class HomeController: UIViewController { @IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var tableView: UITableView! var categorys: [Category] = [] var searchActive : Bool = false var itemInventory: [ItemList] = [] var itemSetup: [ItemList] = [] override func viewDidLoad() { super.viewDidLoad() categorys = Category.createCategoryArray() searchBar.delegate = self } // fetches Firebase Data func fetchProducts(_ completion: @escaping ([ItemList]) -> 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( {ItemList(dictionary: $0.data())} )) } } } extension HomeController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if searchBar.text != "" { return self.itemInventory.count } return itemSetup.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as ItemCell else { return UITableViewCell() } //search not empty if searchBar.text != "" { cell.configure(withProduct: itemInventory[indexPath.row]) }else{ cell.configure(withProduct: itemSetup[indexPath.row]) } return cell } } extension HomeController: 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: "CategoryCell", for: indexPath) as! CategoryCell let category = categorys[indexPath.row] cell.setCategory(category: category) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell", for: indexPath) as! CategoryCell let category = categorys[indexPath.row] cell.setCategory(category: category) print("\(category): \(cell.categoryLbl.text!)") searchBar.text = cell.categoryLbl.text! //added this so I could get the search results from pressing a cell in the collection view } } extension HomeController : UISearchBarDelegate, UISearchDisplayDelegate { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { productInventory = self.itemSetup.filter({ (products) -> Bool in return products.name.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil || products.category.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }) self.tableView.reloadData() } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchActive = true; } func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { searchActive = false; self.searchBar.endEditing(true) } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchActive = false; self.searchBar.endEditing(true) } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchActive = false; self.searchBar.endEditing(true) } }
class CategoryCell: UICollectionViewCell { @IBOutlet weak var categoryLbl: UILabel! @IBOutlet weak var categoryView: UIView! func setCategory(category: Category) { categorylbl.text = category.categoryLabel } } import UIKit import SDWebImage import Firebase class ItemCell: UITableViewCell { weak var product: ProductList! weak var infoDelegate: InfoDelegate? var addActionHandler: ((Int) -> Void)? @IBOutlet weak var productImage: UIImageView! @IBOutlet weak var productName: UILabel! @IBOutlet weak var categoryLabel: UILabel! func configure(withProduct product: ProductList) { productName.text = product.name categoryLabel.text = product.category productImage.sd_setImage(with: URL(string: product.imageUrl)) } override func layoutSubviews() { super.layoutSubviews() } }
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: "Veggies") let category3 = Category(categoryLabel: "Fruit") let category4 = Category(categoryLabel: "Bread") let category5 = Category(categoryLabel: "Drinks") let category6 = Category(categoryLabel: "Dairy") categorys.append(category1) categorys.append(category2) categorys.append(category3) categorys.append(category4) categorys.append(category5) categorys.append(category6) return categorys } } enum Cats: String { case all = "" //to get all results in search case fruit = "Fruit" case veggies = "Veggies" case bread = "Bread" case drinks = "Drinks" case dairy = "Dairy" } import Foundation import UIKit import Firebase import FirebaseFirestore class ItemList { var id: String var name: String var category: String var imageUrl: String Init(id: String, name: String, category: String, imageUrl: String) { self.id = id self.name = name self.category = category self.imageUrl = imageUrl } convenience init(dictionary: [String : Any]) { let id = dictionary["id"] as? String ?? "" let name = dictionary["name"] as? String ?? "" let category = dictionary["category"] as? String ?? "" let imageUrl = dictionary["imageUrl"] as? String ?? "" self.init(id: id, name: name, category: category, imageUrl: imageUrl) } }
我一直试图通过tableview上方的collection视图对我的搜索结果建立索引,以便当我在collection view中按一个单元格时,它可以为表中的结果编制索引,如图所示...
这可能是因为您将过滤后的数组设置为productInventory
,而该行在单元格中没有使用。