我有JSON数据。我正在与SwiftyJSON。我的数据是这样的:Swift:过滤表格中的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
为什么从字符串初始化字符串('title','locationName'等似乎是字符串呢)? – vadian
所以它不会是可选的 – lamfete