0
我真的很掙扎解析這個JSON(https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y)和填充的tableview與「日期」文本,「事件」文本,「閃現」文字和「位置」文本。我意識到這對非Newb來說可能很容易。這是我大概可怕代碼:斯威夫特JSON的tableview
import UIKit
import Foundation
class MasterTableViewController: UITableViewController {
var vigoData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
UINavigationBar.appearance().barTintColor = UIColor(red: 52.0/255.0, green: 170.0/255.0, blue: 220.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
let url = NSURL(string: "https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
if let eventData = jsonResult["Collection2"] as? [[String: AnyObject]] {
for event in eventData {
if let _ = event["Event"] as? String {
self.vigoData.append("text")
}
}
}
print(self.vigoData)
} catch {
print("JSON Serialization failed")
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
您可能想分幾步分割您的任務,並嘗試一次解決每一步:1)提取您需要的數據在一個數據結構(元組數組)中,2)使用數據結構來填充你的表格單元格 – Lachezar
請仔細解釋這個更深入的例子嗎?我是一個業餘的 –
例如嘗試解析JSON文檔並在元組數組中提取所需的值。您可以按照本教程學習如何在Swift中使用JSON:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial;這裏是關於元組的另一個教程:http://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views – Lachezar