首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >搜索栏导致“收集视图”显示错误的图像

搜索栏导致“收集视图”显示错误的图像
EN

Stack Overflow用户
提问于 2019-06-27 05:10:17
回答 1查看 0关注 0票数 0

当我搜索时,衣服名称正确缩小,但衣服图像保持不变。我理解这是因为集合视图正在显示第一个单元格的图像,我想知道我如何能够更改第一个单元格的图像以匹配正在搜索的衣服名称。

代码语言:javascript
复制
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.item]
    } 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()
}

}

EN

回答 1

Stack Overflow用户

发布于 2019-06-27 14:48:41

代码语言:javascript
复制
if searching {
    cell.clothingLabel.text = searchingName[indexPath.item]
} else {
    cell.clothingLabel.text = clothingName[indexPath.item]
    cell.clothingImageView.image = clothingImage[indexPath.item]
}

您还没有设置图像searching == true。您需要找到clothingImage与搜索字符串匹配的索引。

建议

创建衣服作为字典。

代码语言:javascript
复制
let clothings = [
    ["name":"Yeezy","image":UIImage(named: "yeezy")],
    ["name":"Bape","image":UIImage(named: "bape")]
]

因此,在搜索时您可以过滤以创建新的数据源

代码语言:javascript
复制
let dataSource = clothings.filter { $0["name"] == query }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

代码语言:javascript
复制
return dataSource.count

然后在func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 你可以设置你的细胞

代码语言:javascript
复制
cell.clothingLabel.text = dataSource[indexPath.row]["name"]
cell.clothingImageView.image = dataSource[indexPath.row]["image"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档