ios - Swift:过滤 tableView 中的 SwiftyJSON 数据

标签 ios swift uitableview uisearchcontroller swifty-json

我有 JSON 格式的数据。我正在使用 SwiftyJSON。我的数据是这样的:

[{
    "kode_customer": 1,
    "nama_customer": "Logam Jaya, UD",
    "alamat_customer": "Rajawali No 95",
    "kodepos": 60176,
    "kode_provinsi": 11,
    "gps_lat": -7.233834999999999,
    "gps_long": 112.72964666666667
}, {
    "kode_customer": 2,
    "nama_customer": "Terang, TK",
    "alamat_customer": "Raya Dukuh Kupang 100",
    "kodepos": 60225,
    "kode_provinsi": 11,
    "gps_lat": -7.285430000000001,
    "gps_long": 112.71538333333335
}, {
    "kode_customer": 3,
    "nama_customer": "Sinar Family",
    "alamat_customer": "By Pass Jomin No 295",
    "kodepos": 41374,
    "kode_provinsi": 9,
    "gps_lat": -6.4220273,
    "gps_long": 107.4748978
}, {
    "kode_customer": 4,
    "nama_customer": "Lancar Laksana, TB",
    "alamat_customer": "Jendral Sudirman No 69",
    "kodepos": 41374,
    "kode_provinsi": 9,
    "gps_lat": -6.4220273,
    "gps_long": 107.4748978
}]

如何在 tableView 上显示它并使用 UISearchController 过滤它。这是我的代码:

class LocationSearchTable: UITableViewController {

    var custData: JSON = []   
    var filteredData: [String]!

    func getCustData() {
        let path = NSBundle.mainBundle().pathForResource("cust_toko", ofType: "json")
        let jsonData = NSData(contentsOfFile: path!)
        let json = JSON(data: jsonData!)

        self.custData = son
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
        cell.textLabel?.text = filteredData[indexPath.row]
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(filteredData[indexPath.row])
    }
}

extension LocationSearchTable: UISearchResultsUpdating {
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        if let searchText = searchController.searchBar.text {
            let searchPredicate = NSPredicate(format: "name contains[cd] %@", searchText)
            filteredData = JSON(custData.filter{ JSON.Value() })

            tableView.reloadData()
        }
    }
}

出现这样的错误:“Value”(又名“AnyObject”)无法构造,因为它没有可访问的初始化器

这是这个问题的 github 链接 https://github.com/lamfete/MapSearchDemo

最佳答案

首先,您需要使用 arrayObject 从 JSON 对象获取数组,然后您需要将谓词键 name 更改为 customer_name 因为那里在您的 json 响应中没有 key 名称,因此像这样更改您的代码以仅从该结果中获取 customer_name。

extension LocationSearchTable: UISearchResultsUpdating {
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        if let searchText = searchController.searchBar.text {
            let searchPredicate = NSPredicate(format: "nama_customer contains[cd] %@", searchText)
            if let array = custData.arrayObject as? [[String:AnyObject]] {
                 let filterArr = array.filter{ searchPredicate.evaluateWithObject($0) }
                 filteredData = filterArr.map({ String($0["nama_customer"])})
                 tableView.reloadData()
            }
        }
    }
}

关于ios - Swift:过滤 tableView 中的 SwiftyJSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650438/

相关文章:

swift - 如何在 swift 中使用可变闭包?

iphone - 使用 ASIHTTPRequest 的 UITableViewCell 中的 UIImageView 性能

objective-c - 在 iPad 应用程序中隐藏 UITabBar

swift - 从枚举中获取关联值,无需 switch/case

ios - 无法从 iPhone 上的内部服务器安装企业应用程序 ( IOS 11.4)

ios - 如何在每次按下 UIButton 时创建一个新的 UILabel?

ios - didSelectRowAtIndexPath 仅通过长按嵌入在容器 View 中的 tableView 调用

ios - 如何获得滚动移动的长度

iphone - 向上移动 View ,使文本字段不会消失在键盘后面

ios - Swift 4 中的布局 anchor