搜索栏过滤器结果在“收藏夹”视图中显示正确的名称,但图像不正确

时间:2019-06-29 19:54:08

标签: xcode

在搜索时,正确缩小了clothingName的范围,但是clothingImage保持不变。我注意到,仅出现的图像是我的clothingImage数组(“最高”)中的第一张图像。

import UIKit

class SecondViewController:UIViewController,UICollectionViewDelegate, UICollectionViewDataSource,UISearchBarDelegate {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if searching {
            return searchingName.count
        } else {
            return clothingName.count
        }
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
        let clothingImageView = cell.viewWithTag(1) as! UIImageView
        clothingImageView.image = clothingImage[indexPath.row]


        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 0.5

        if searching {
            cell.clothingLabel.text = searchingName[indexPath.row]
        } else {
            cell.clothingLabel.text = clothingName[indexPath.item]
            cell.clothingImageView.image = clothingImage[indexPath.item]
        }

        return cell

    }

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var searchBar: UISearchBar!

    var clothingName = ["Supreme","Yeezy","Bape","Off-White","Jordan","Palace","LV","Adidas"]

    var searchingName = [String()]

    var searching = false

    var arrayOfIDs = [String]()

    var clothingImage: [UIImage] = [

        UIImage(named: "supreme")!,
        UIImage(named: "yeezy")!,
        UIImage(named: "bape")!,
        UIImage(named: "off-white")!,
        UIImage(named: "jordan")!,
        UIImage(named: "palace")!,
        UIImage(named: "lv")!,
        UIImage(named: "adidas")!,


        ]

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.dataSource = self
        collectionView.delegate = self
        searchBar.delegate = self


        let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.sectionInset = UIEdgeInsets(top: 0,left: 5,bottom: 5,right: 5)
        layout.minimumInteritemSpacing = 5
        layout.itemSize = CGSize(width: (self.collectionView.frame.size.width - 20)/2, height: self.collectionView.frame.size.height/3)

        arrayOfIDs = ["supreme","yeezy"]

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.layer.borderColor = UIColor.gray.cgColor
        cell?.layer.borderWidth = 3

        let name = arrayOfIDs[indexPath.row]
        let viewController = storyboard?.instantiateViewController(withIdentifier: name)
        self.navigationController?.pushViewController(viewController!, animated: true)

    }


    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.layer.borderColor = UIColor.lightGray.cgColor
        cell?.layer.borderWidth = 0.5
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchingName = clothingName.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
        searching = true
        collectionView.reloadData()
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searching = false
        searchBar.text = ""
        searchBar.endEditing(true)
        collectionView.reloadData()
    }

}

0 个答案:

没有答案