UIKit SearchBar 在 textDidChange 中过滤两个数组

UIKit SearchBar Filter Two Arrays in textDidChange

我有两个初始化和未初始化的数组如下:

var colours: [String] = ["Blue", "Red", "Green", "Yellow"]
var numbers: [Int]    = [11, 12, 13, 14]
var coloursFiltered: [String]?
var numbersFiltered: [Int]?

在我的搜索栏中,我只能过滤 textDidChange 函数中的一个数组,如下所示:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        coloursFiltered = colours.filter({ item -> Bool in
            if searchText.isEmpty { return true }
            return item.lowercased().contains(searchText.lowercased())
        })
        tableView.reloadData()
}

/* Later in the Code */
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return coloursFiltered!.count
}

我还需要过滤 numbers 数组以匹配过滤后的 colours 数组的相应索引。我不能这样做,因为我无法访问 colours.filter 函数中的当前索引。这是为了达到以下目的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    self.tableView.register(UINib.init(nibName: "ItemTableViewCell", bundle: nil), forCellReuseIdentifier: "ItemTableViewCell")
    
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "ItemTableViewCell", for: indexPath) as! ItemTableViewCell
   
    cell.name.text = "\(coloursFiltered![indexPath.row])"
    cell.code.text = ""\(numbersFiltered![indexPath.row])"   // I need this to work
   
    return cell
}

有没有办法使用相同的字符串过滤两个数组?

你最好有个模型

struct Item {
  let color:String
  let number:Int
}

然后

var all = [Item(color:"Blue",number:11),.....]
var coloursFiltered = [Item]()

然后过滤它

coloursFiltered = all.filter({ item -> Bool in
  if searchText.isEmpty { return true }
     return item.color.lowercased().contains(searchText.lowercased())
  })
tableView.reloadData()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return coloursFiltered.count
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    self.tableView.register(UINib(nibName: "ItemTableViewCell", bundle: nil), forCellReuseIdentifier: "ItemTableViewCell") 
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "ItemTableViewCell", for: indexPath) as! ItemTableViewCell
    let item = coloursFiltered[indexPath.row]
    cell.name.text = item.color
    cell.code.text = "\(item.number)"  
    return cell
}

我尝试了 and it is working and very helpful. Here is a similar approach to the solution that I found in another question inspired by @Aju Antony 的答案。

 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {  
    
    let result = zip(colours, numbers).filter { name, code in
        name.lowercased().contains(searchText.lowercased()) ||
        String(code).contains(searchText.lowercased())
    }
    
    let filteredNames = result.map({ [=10=].0 })
    let filteredCodes = result.map({ [=10=].1 })
    
    self.coloursFiltered = filteredNames
    self.numberseFiltered = filteredCodes
    
    tableView.reloadData()
}