微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我无法使用搜索栏快速搜索/过滤图像

如何解决我无法使用搜索栏快速搜索/过滤图像

我在顶部有一个tableView和一个搜索栏,自定义单元格具有一个ImageView和一个标签,文本是从字符串Array提取的,图片是从UIImage数组提取的,但是当我尝试过滤条目时,尽管我有通过字符串实现了这些功能,但是却无法使用图像。搜索时得到的是相关的字符串条目(例如,三个以'c'开头的条目),但是经过过滤的图像条目是UIImage中的前三个图像例如数组。

import UIKit

let firstArray = [ "APC","Apple","Autonomic","Axis","B&W","BSS","Central","Sisco","Crestron","Crown","Denon","Digital","Direct Tv"]

class FirstTableViewController: UITableViewController,UISearchBarDelegate {
    //for search bar
    @IBOutlet weak var searchBar: UISearchBar!
  
    var filteredData: [String]!
    var filteredImages: [UIImage]!
    var searching = false

    var firstArray = [String]()

    var images = [UIImage(named:"APC"),UIImage(named:"Apple"),UIImage(named:"Autonomic"),UIImage(named:"Axis"),UIImage(named:"B&W"),UIImage(named:"BSS"),UIImage(named:"Central"),UIImage(named:"Sisco"),UIImage(named:"Crestron"),UIImage(named:"Crown"),UIImage(named:"Denon"),UIImage(named:"Digital"),UIImage(named:"Direct Tv")]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //for search bar
        searchBar.delegate = self
        filteredData = firstArray
     
        firstArray = [ "APC","Cisco","Direct Tv"]
    }
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation,return the number of rows
        if searching
        {
            return filteredData.count
        }else{
            return firstArray.count
        }
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! TableTableViewCell

        Cell.textLabel?.textColor = .systemBlue
        Cell.textLabel?.font = .boldSystemFont(ofSize: 20)
        
        if searching
        {
            Cell.textLabel?.text = filteredData[indexPath.row]
            Cell.imageManuf?.image = self.images[indexPath.row]
            
        }else{
            Cell.textLabel?.text = firstArray[indexPath.row]
            Cell.imageManuf?.image = self.images[indexPath.row]
        }
        
        
        return Cell
        
    }

    //Mark: Search Bar config
    func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
        
        filteredData = firstArray.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
        
        searching = true

        
        //to reload the data
        self.tableView.reloadData()
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searching = false
        searchBar.text = ""
        tableView.reloadData()
    }
}

您能帮忙吗?我在图像方面遇到各种各样的问题,我似乎对它们没有把握。

解决方法

您应该创建一个结构,以便将每个字符串与相应的图像“分组”。每对“字符串图像”都代表一个东西,对吧?

struct ImageWithName { // please think of a better name than this
    let image: UIImage
    let name: String
    init(name: String) {
        self.name = name
        image = UIImage(named: name)!
    }
}

您可以只存储一个ImageWithName数组,而不是存储两个数组,一个用于字符串,一个用于图像。

var filteredData: [ImageWithName]!
var searching = false

var firstArray = [ImageWithName]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    //for search bar
    searchBar.delegate = self
 
    firstArray = ["APC","Apple","Autonomic","Axis","B&W","BSS","Central","Cisco","Crestron","Crown","Denon","Digital","Direct Tv"]
        .map(ImageWithName.init(name:))
    filteredData = firstArray
}

cellForRowAt中,访问.name.image

if searching
{
    cell.textLabel?.text = filteredData[indexPath.row].name
    cell.imageManuf?.image = filteredData[indexPath.row].image
        
}else{
    cell.textLabel?.text = firstArray[indexPath.row].name
    cell.imageManuf?.image = firstArray[indexPath.row].image
}

textDidChange中,访问.name以搜索名称:

func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
    filteredData = firstArray.filter({$0.name.lowercased().prefix(searchText.count) == searchText.lowercased()})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。