gpt4 book ai didi

ios - 即使使用 reloadData,UICollectionView 也不会重新加载搜索结果

转载 作者:行者123 更新时间:2023-12-01 19:35:44 31 4
gpt4 key购买 nike

我可以获得搜索功能来过滤掉结果,但它不会在 iPhone 模拟器上显示那些搜索结果。它在我拥有的 iPhone(iPhone 5)上显示结果,但不在模拟器上。我不知道为什么我输入时他们的 Collection View 没有更新。数组中的项目正在显示,因为我首先导航到该屏幕我可以看到它们。

my screenshot

import UIKit



class BrowseViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate {




@IBOutlet weak var testCollectionView: UICollectionView!



var Array = [Product]()
var currentArray = [Product]() //update table





private func setUpAnimals() {

Array.append(Product(name: "Cheetos", price: "3.99", image:"1"))
Array.append(Product(name: "Coca Cola", price: "2.29", image:"2"))
Array.append(Product(name: "Red Bull", price: "3.79", image:"3"))
Array.append(Product(name: "Doritos", price: "1.99", image:"4"))
Array.append(Product(name: "Lays Barbecue", price: "1.89", image:"5"))
Array.append(Product(name: "Skittles", price: "1.49", image:"6"))
Array.append(Product(name: "Cheez It", price: "1.29", image:"7"))
Array.append(Product(name: "Pringles", price: "3.49", image:"8"))
Array.append(Product(name: "Lays", price: "1.89", image:"9"))
Array.append(Product(name: "Fritos", price: "3.99", image:"10"))


currentArray = Array

}



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
setUpAnimals()
self.view.backgroundColor = UIColor.white
let itemSize = UIScreen.main.bounds.width/3 - 3

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets.init(top: 30, left: 0, bottom: 20, right: 0)
layout.itemSize = CGSize (width: itemSize, height: itemSize)

layout.minimumInteritemSpacing = 3
layout.minimumLineSpacing = 3

testCollectionView.collectionViewLayout = layout



}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}








// Number of views in the collection view

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.currentArray.count
}



//Populating the viewsc

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

// Shows Image
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TestCollectionViewCell
cell.testImageView.image = UIImage(named: currentArray[indexPath.row].image + ".jpg")
cell.titleLabel.text = (self.currentArray[indexPath.row].name)
cell.priceLabel.text = ("$" + self.currentArray[indexPath.row].price)

return cell
}






// Search Bar
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

currentArray = Array.filter({ Product -> Bool in
switch searchBar.selectedScopeButtonIndex {
case 0:
if searchText.isEmpty { return true }
return Product.name.lowercased().contains(searchText.lowercased())
default:
return false
}
})
//self.testCollectionView.reloadData()
DispatchQueue.main.async { [weak self] in
self?.testCollectionView.reloadData()
}


}

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
switch selectedScope {
case 0:
currentArray = Array

default:
break
}
DispatchQueue.main.async { [weak self] in
self?.testCollectionView.reloadData()
}

}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let VC = storyboard?.instantiateViewController(withIdentifier: "AddToCartViewController") as? AddToCartViewController
VC?.image = UIImage(named: currentArray[indexPath.row].image + ".jpg")!
VC?.name = (self.currentArray[indexPath.row].name)
VC?.price = (self.currentArray[indexPath.row].price)
self.navigationController?.pushViewController(VC!, animated: true)

}



}

最佳答案

问题是您没有设置委托(delegate),因此不会调用搜索栏回调方法!

如果您使用 SearchController,则需要将此行添加到 viewDidLoad

navigationItem.searchController?.searchBar.delegate = self

否则,将 searchBar 委托(delegate)设置为它
searchBar.delegate = self

注意:您应该更改 Array将名称更改为遵循 Camel Case 命名约定的更具描述性的名称,例如 filteredArray

关于ios - 即使使用 reloadData,UICollectionView 也不会重新加载搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243908/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com