Swift:在tableView中过滤SwiftyJSON数据

兰费特

我有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' (aka 'AnyObject') cannot be constructed because it has no accessible initializers

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

兰费特

我想回答我自己的问题。

import Foundation
import UIKit
import SwiftyJSON
import MapKit

class LocationSearchTable: UITableViewController {

    var custData: JSON = []
    var dbTokos : [Toko] = []
    var filteredDataToko: [Toko]!

    var handleMapSearchDelegate: HandleMapSearch? = nil
    var vc = ViewController()

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

        self.custData = Jon

        splitData()
}

    func splitData() {
        for(_, subJson):(String, JSON) in self.custData {
            if let name: String = subJson["nama_customer"].stringValue {
                if let address: String = subJson["alamat_customer"].stringValue {
                    if let city: String = subJson["kota"].stringValue {
                        if let province: String = subJson["provinsi"].stringValue {
                            if let lat: Double = subJson["gps_lat"].doubleValue {
                                if let long: Double = subJson["gps_long"].doubleValue {
                                    let toko = Toko(title: name,
                                                locationName: address,
                                                cityName: city,
                                                provinsiName: province,
                                                coordinate: CLLocationCoordinate2D(latitude: lat, longitude: long))
                                    dbTokos.append(toko)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
        cell.textLabel!.text = String(filteredDataToko[indexPath.row].title!)
        cell.detailTextLabel!.text = String(filteredDataToko[indexPath.row].locationName) + ", " + String(filteredDataToko[indexPath.row].cityName) + ", " + String(filteredDataToko[indexPath.row].provinsiName)
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let toko = Toko(title: filteredDataToko[indexPath.row].title!,
                        locationName: filteredDataToko[indexPath.row].locationName,
                        cityName: filteredDataToko[indexPath.row].cityName,
                        provinsiName: filteredDataToko[indexPath.row].provinsiName,
                        coordinate: filteredDataToko[indexPath.row].coordinate)

        handleMapSearchDelegate?.dropPinZoomIn(toko)
        dismissViewControllerAnimated(true, completion: nil)
    }
}

extension LocationSearchTable: UISearchResultsUpdating {
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        if let searchText = searchController.searchBar.text {
            filteredDataToko = searchText.isEmpty ? dbTokos : dbTokos.filter(
                {
                    (dataString: Toko) -> Bool in
                    return dataString.title?.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
                }
            )
            tableView.reloadData()
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在TableView中更新数据(Swift)

来自分类Dev

tableView Firebase Swift 中的异步数据

来自分类Dev

如何在Swift 3.0中使用SwiftyJSON将JSON数据分配到UILabel中?

来自分类Dev

Swift 2 SwiftyJSON API数据到UITableView

来自分类Dev

Swift TableView数据填充

来自分类Dev

TableView解析Swift数据未显示在应用程序中

来自分类Dev

tableView数据源在viewDidLoad()中为空-Swift

来自分类Dev

Tableview不会在Swift 2中显示数据

来自分类Dev

在Swift 5中将json数据提取到tableview

来自分类Dev

TableView解析Swift数据未显示在应用程序中

来自分类Dev

在OS X Tableview中显示的Swift核心数据

来自分类Dev

在Swift中,从JSON加载的tableView数据仅加载80%的时间

来自分类Dev

Swift 3:无法从 tableView 中删除重复数据

来自分类Dev

如何使用SwiftyJSON从文件中检索数据

来自分类Dev

使用swiftyJSON和sqlite.swift将基于json的数据添加到sqlite数据库中

来自分类Dev

Swift iOS中的tableView

来自分类Dev

iOS - 将 tableView 中的 numberOfSections 设置为过滤单元格的数量 (Swift)

来自分类Dev

RxSwift-从UIPickerView的tableview过滤数据

来自分类Dev

如何过滤swiftyjson数组

来自分类Dev

需要帮助来过滤Swift 3中的核心数据

来自分类Dev

PickerView加载tableview数据Swift

来自分类Dev

以Xcode / Swift导出TableView数据

来自分类Dev

尝试使用 SwiftyJSON 使用 swift 3 解析 JSON 数据

来自分类Dev

在swift MVC(Swift)中保存tableview数据

来自分类Dev

在Swift中过滤元组

来自分类Dev

tableview会使应用程序变慢,并花费一些时间来获取数据-swiftyJSON

来自分类Dev

在Tensorflow中过滤数据

来自分类Dev

过滤数据中的“峰值”

来自分类Dev

在DataGridViewer中过滤数据

Related 相关文章

热门标签

归档