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

时间:2019-02-20 18:54:32

标签: ios swift uitableview

我的场景是,我创建的UITableView具有以下结构化的JSON数据加载。我需要为globalfirstnamepricedatetitle添加description搜索。在这里,我尝试了一下,但对我来说效果并不理想。请给我一些解决方案。

例如:用户searchednamepricedatetitledescription组成的UITableView应该分类列表。

我的可编码结构

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
}

我的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
    }

我的搜索条形码

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()
        }
    }

1 个答案:

答案 0 :(得分:0)

在结构基准中,您的CodingKeys与属性名称不匹配

  

枚举案例的名称应与您为类型中的相应属性指定的名称相匹配。

如果您这样更新结构:

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"
    }
}

稍后您可以像这样过滤元素数组:

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")}