【问题标题】:Swift SearchBar Filtering & Updating Multiple ArraysSwift SearchBar 过滤和更新多个数组
【发布时间】:2017-01-20 08:06:06
【问题描述】:

我需要实现一个 searchBar 来搜索和过滤带有 2 个标签的 tableview。标记来自 2 个不同数组的数据。因此,当我通过数组 1/标签 1 过滤时,它会过滤但标签 2 保持不变,因此结果是混合的。这两个数组都是在 SQL Query 结果后创建的,包含 2 列数据。我的意思是 arr1[0] 和 arr2[0] 是同一行但不同的列。经过太多次尝试后我被卡住了。这是最新的代码:

var arr1 = [String]()
var arr2 = [String]()
var filtered:[String] = []

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(searchActive) {
        return filtered.count
    } else {
        return arr1.count
    }
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> xxTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "xxCell", for: indexPath) as! xxTableViewCell

    if(searchActive){

        cell.label1.text = filtered[(indexPath as NSIndexPath).row]

    } else {

        cell.label1.text = arr1[(indexPath as NSIndexPath).row]
        cell.label2.text = arr2[(indexPath as NSIndexPath).row]

    }
    return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = arr1.filter({ (text) -> Bool in
        let tmp: NSString = text as NSString
        let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }


    self.tableView.reloadData()
}

【问题讨论】:

    标签: ios arrays swift uitableview uisearchbar


    【解决方案1】:

    如果 arr1[] 和 arr2[] 是单行数据的两列,那么你应该有一个数组。有很多方法可以做到这一点——元组、类、结构——但我倾向于使用结构。如果您有其他需要执行的处理,则可以将其作为一个类更好地实现,但同样的原则也适用。

    定义你需要的结构

    struct MyDataStruct
    {
        var label1 : String = ""
        var label2 : String = ""
    }
    

    然后定义一个这种类型的数组(而不是arr1,arr2)

    var myData = [MyDataStruct]()
    

    然后像以前一样构建数据和搜索数组 - 但在这个单一结构中

    myData.append(MyDataStruct(label1: "Hello", label2: "World"))
    

    最后一步是在 tableView 方法中

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> xxTableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "xxCell", for: indexPath) as! xxTableViewCell
    
        if(searchActive){
            cell.label1.text = filtered[indexPath.row].label1
        } else {
            cell.label1.text = myData[indexPath.row].label1
            cell.label2.text = myData[indexPath.row].label2 
        }
        return cell
    }
    

    【讨论】:

      【解决方案2】:

      问题:searchActive = true 时,label2.textcellForRow 中没有任何价值。因此,每当您在过滤器之后重新加载 tableView 时,只需更新 label1 的新值。

      解决方案:

      像这样修改你的代码。

      if(searchActive){
      
          cell.label1.text = filtered[(indexPath as NSIndexPath).row]
          cell.label2.text = @"" //assign value to label2 after filter 
      } else {
      
          cell.label1.text = arr1[(indexPath as NSIndexPath).row]
          cell.label2.text = arr2[(indexPath as NSIndexPath).row]
      
      }
      

      【讨论】:

      • 问题是我不知道如何在过滤后为 label2 赋值
      • 我以前试过这个。它搜索和过滤两个数组,因此过滤的结果不准确。我的意思是当用户搜索一个字符串时,它可能会过滤掉不必要的结果
      • 它从两个字符串中过滤相同的文本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多