Ios 快速搜索栏过滤&;更新多个阵列

Ios 快速搜索栏过滤&;更新多个阵列,ios,arrays,swift,uitableview,uisearchbar,Ios,Arrays,Swift,Uitableview,Uisearchbar,我需要实现一个搜索栏,搜索和过滤带有2个标签的tableview。标记来自2个不同阵列的数据。所以当我过滤数组1/标签1时,它会过滤,但标签2保持不变,所以结果是混合的。这两个数组都是在包含两列数据的SQL查询结果之后创建的。我的意思是,arr1[0]和arr2[0]是同一行,但列不同。我试了太多次都卡住了。以下是最新代码: var arr1 = [String]() var arr2 = [String]() var filtered:[String] = [] override func

我需要实现一个搜索栏,搜索和过滤带有2个标签的tableview。标记来自2个不同阵列的数据。所以当我过滤数组1/标签1时,它会过滤,但标签2保持不变,所以结果是混合的。这两个数组都是在包含两列数据的SQL查询结果之后创建的。我的意思是,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()
}
问题:
searchActive=true
时,
cellForRow
中的
label2.text
没有值。因此,每当在筛选后重新加载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]

}
问题:
searchActive=true
时,
cellForRow
中的
label2.text
没有值。因此,每当在筛选后重新加载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]

}

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

定义所需的结构

struct MyDataStruct
{
    var label1 : String = ""
    var label2 : String = ""
}
然后定义此类型的数组(而不是arr1、arr2)

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

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
}

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

定义所需的结构

struct MyDataStruct
{
    var label1 : String = ""
    var label2 : String = ""
}
然后定义此类型的数组(而不是arr1、arr2)

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

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
}

问题是我不知道在filteri之前尝试过这个之后,我应该如何为label2赋值。它通过两个数组进行搜索和筛选,因此筛选的结果不准确。我的意思是,当用户搜索一个字符串时,它可能会过滤不必要的结果。它会从两个字符串中过滤相同的文本。问题是我不知道如何在过滤后为label2赋值。我以前尝试过这个方法。它通过两个数组进行搜索和筛选,因此筛选的结果不准确。我的意思是,当用户搜索字符串时,它可能会过滤不必要的结果。它会从两个字符串中过滤相同的文本。