香港专业教育学院一直试图通过我的tableview上方的集合视图索引我的搜索结果,以便当我在集合视图中按一个单元格可以索引表中的结果,如下图所示
到目前为止,代码的工作范围是:一旦我在集合视图中按下一个单元格,它就会将所选类别的文本放置在搜索栏中,并获得搜索结果,但是稍后,模拟器会崩溃,错误线程1:致命错误:tableview的cellForRowAt中cell.configure(withProduct: itemInventory[indexPath.row])
的索引超出范围
在此先感谢您的帮助
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)
}
}
最佳答案
这可能是因为您将过滤后的数组设置为productInventory
,在“单元格”行中未使用该数组。
关于ios - 如何获取集合 View 以控制tableview中的搜索结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59506392/