简体   繁体   English

如何使用Swift 4.2在TableView中为JSON可编码结构数据创建搜索功能?

[英]How to create search function for JSON codable structure data in TableView using Swift 4.2?

My scenario, I created UITableView with below structured JSON data load. 在我的场景中,我创建了具有以下结构化JSON数据加载的UITableView I need to add global search for firstname , price , date , title and description . 我需要为firstnamepricedatetitledescription添加global搜索。 Here, I tried but it didn't worked well for me. 在这里,我尝试了一下,但是对我来说效果并不理想。 Please give me some solution. 请给我一些解决方案。

For eg: User searched by name or price , date , title and description UITableView should sortout list. 例如:按nameprice searched的用户, datetitledescription UITableView应该分类列表。

My Codable Struture 我的可编码结构

struct Welcome: Codable {
    let status: Int
    let message: String
    let ode: Int
    let data: [Datum]
}

struct Datum: Codable {
    let id: Int
    let title, description: String
    let price: Int
    let user: User
    let Appcode: Appcode
    let Appbase: Appbase

    enum CodingKeys: String, CodingKey {
        case id
        case userID = "userid"
        case title, description
        case price
        case Date = "date"
        case location, user
        case appcode = "appcode”
        case appbase = "appbase”
    }
} 

struct Appcode: Codable {
    let card: String
}

struct Appbase: Codable {
    let base: String
}

struct User: Codable {
    let firstname, lastname: String
}

My Tableview Code 我的Tableview代码

    var tableArray = [Datum]()
    var filteredResults = [Datum]()
    lazy var searchBar:UISearchBar = UISearchBar() 
    var isSearching = false

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

        if isSearching {
            return filteredResults.count
        } else {
            return self.tableArray.count
        }
        return 0
    }

My Search bar code 我的搜索条码

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

        if searchBar.text == nil || searchBar.text == "" { //
            isSearching = false
            view.endEditing(true)
            tableView.reloadData()
        }
        else {
            isSearching = true
            filteredResults = tableArray.filter({ value -> Bool in
                guard let text =  searchBar.text else { return false}
                return value.description.contains(text) // According to title from JSON
            })
            tableView.reloadData()
        }
    }

In your struct Datum your CodingKeys don't match your property names 在您的结构基准中,您的CodingKeys与您的属性名称不匹配

The names of the enumeration cases should match the names you've given to the corresponding properties in your type. 枚举案例的名称应与您为类型中的相应属性指定的名称相匹配。

If you update your struct like this: 如果您这样更新结构:

struct Datum: Codable {
    let id: Int
    let title, description: String
    let price: Int
    let user: User
    let appCode: Appcode
    let appBase: Appbase

    enum CodingKeys: String, CodingKey {
        case id
        case title, description
        case price
        case user
        case appCode = "appcode"
        case appBase = "appbase"
    }
}

You can later filter the array of elements like this: 您以后可以过滤元素数组,如下所示:

let datum1 = Datum(id: 1, title: "test", description: "test1", price: 10, user: User(firstname: "a", lastname: "b"), appCode: Appcode(card: "app"), appBase: Appbase(base: "base"))

let datum2 = Datum(id: 1, title: "test", description: "test2", price: 10, user: User(firstname: "a", lastname: "b"), appCode: Appcode(card: "app"), appBase: Appbase(base: "base"))

let datum3 = Datum(id: 1, title: "test", description: "test3", price: 10, user: User(firstname: "a", lastname: "b"), appCode: Appcode(card: "app"), appBase: Appbase(base: "base"))

let array = [datum1, datum2, datum3]
array.filter{$0.user.firstname == "a"}
array.filter{$0.description.contains("2")}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM