过滤/搜索嵌套结构数组

时间:2016-06-27 16:09:55

标签: ios arrays swift search filter

我试图将搜索栏添加到分组表的顶部。但是,我不确定如何过滤我的数据。数据存储在嵌套数组中,对象以这种格式保存;

struct Group {
    var id: String
    var type: String
    var desc: String
    var avatar: String
    var name: String

    init() {
        id = ""
        type = ""
        desc = ""
        avatar = ""
        name = ""
    }
}

因为我从两个源获取数据,所以两个数组嵌套在一起,这也使得创建分组表的两个部分变得更加简单。我注意到,它们都使用相同的Group结构。

self.masterArray = [self.clientArray, self.departmentArray]

这个" masterArray"然后用于填充表格。过滤/搜索单个数组并不困难,但如何搜索嵌套数组呢?

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

}

修改 @appzYourLife,我终于让事情变得有效了。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    print("Searching for:" + searchText)
    if searchText.isEmpty {
        filterArray = masterArray
    } else {
        filterArray = [ clientArray.filter { $0.name.range(of: searchText) != nil }] + [ departmentArray.filter { $0.name.range(of: searchText) != nil } ]
    }
    tableView.reloadData()
}

3 个答案:

答案 0 :(得分:3)

您可以使用<div class="container"> <div class="row row-eq-height"> <div class="col-md-4"> ddddddsadddddddsadddddddsadddddddsa <br>ddddddsadddddddsadddddddsadddddddsa <br>ddddddsadddddddsadddddddsadddddddsa <br> <div class="inner-div"> adsadadadada </div> </div> <div class="col-md-4"> ddddddsadddddddsadddddddsadddddddsa <div class="inner-div"> asdadadaada </div> </div> <div class="col-md-4"> ddddddsadddddddsadddddddsadddddddsa <br>ddddddsadddddddsadddddddsadddddddsa <br>ddddddsadddddddsadddddddsadddddddsa <br> <div class="inner-div"> asdadaadas </div> </div> </div> </div>展平您的数组,然后根据您要使用的搜索条件对其进行过滤。 E.g。

.flatten()

但是,根据您的问题的编辑,您似乎希望生成的过滤数组与“主”数组具有相同的嵌套数组类型。在这种情况下,以下是更合适的方法:

struct Group {
    var id: String
    var name: String
    init(_ id: String, _ name: String) { self.id = id; self.name = name }
    /* .... */
}

let clientArray = [Group("a", "John"), Group("b", "Jane"), Group("c", "Phil")]
let departmentArray = [Group("a", "Foo"), Group("b", "Bar"),
                       Group("c", "Baz"), Group("d", "Bax")]

let arr = [clientArray, departmentArray]

// find some id
let searchForId = "c"
let hits = arr.flatten()
    .filter { $0.id.lowercaseString.containsString(searchText.lowercaseString) }

print(hits)
    // [Group(id: "c", name: "Phil"), Group(id: "c", name: "Baz")]

答案 1 :(得分:2)

鉴于

struct Group {
    let id: String = ""
    let type: String = ""
    let desc: String = ""
    let avatar: String = ""
    let name: String = ""
}

let clients = [Group(), Group(), Group(), Group()]
let departmens = [Group(), Group(), Group(), Group()]

let clientsAndDepartments = [clients, departmens]

您可以在clientsdepartment内写作

let results = (clients + departmens).filter { $0.id == "123" }

enter image description here

更新#1

现在明白你想过滤两个数组,但结果你仍然想要这样的[[Group]]

所以这是代码

var filterArray = [clients.filter { $0.name == "White" }] + [departmens.filter { $0.name == "White" }]

更新#2

如果要搜索字符串包含,请使用此代码

var filterArray = [ clients.filter { $0.name.rangeOfString("White") != nil }] + [ departmens.filter { $0.name.rangeOfString("White") != nil } ]

答案 2 :(得分:1)

您可以映射每个阵列,并单独过滤它们:

self.masterArray.map{ subarray in 
    subarray.filter { element in
        trueWhenElementShouldStay(element)
    }
}

P.S。我怀疑masterArray不应该是一个实例变量,而是更适合作为局部变量。