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

UICollectionView is not reloading search results even with reloadData

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

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)

    }



}

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

如果你使用SearchController,你需要将这一行添加到viewDidLoad

navigationItem.searchController?.searchBar.delegate = self

否则,将 searchBar 委托设置给它

searchBar.delegate = self

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