【发布时间】:2017-05-17 01:25:02
【问题描述】:
我正在构建一个 CTA 火车跟踪应用程序。我有一个 TableView 填充了每条火车线路,我试图弄清楚如何从单击其中一条火车线路的 TableView 单元格到显示解析的 JSON 数据(目的地站和到达时间)的 TableView那条特定的火车线路。
我想知道最好的方法是什么;最简单的似乎我会从每个单击的单元格到每个火车线路的完全不同的 TableView,但我不确定这是否有效。否则,我似乎需要以某种方式将信息(例如 JSON 提要地址)从每个单击的单元格传递到目标 TableView。有人有什么建议吗?以下是我的代码:
所有火车线路的初始屏幕
import UIKit
import SwiftyJSON
class TableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.navigationItem.title = "CTA Train Tracker"
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trains.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "subtitle", for: indexPath)
let train = trains[indexPath.row]
cell.textLabel?.text = train.name
cell.imageView?.image = UIImage(named: train.image)
return cell
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPathForSelectedRow
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
}
将 JSON 解析为 TableView
import UIKit
let feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=keygoeshere&mapid=41320&outputType=JSON"
class TableViewController: UITableViewController {
class Destinations {
var destination: String = ""
var time: String = ""
}
var records = [Destinations]()
override func viewDidLoad() {
super.viewDidLoad()
parseData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return records.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let destinationRow = records[indexPath.row]
cell.textLabel?.text = destinationRow.destination
cell.detailTextLabel?.text = destinationRow.time
return cell
}
func parseData() {
guard let feedURL = URL(string: feed) else {
return
}
let request = URLRequest(url: feedURL)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
print("Error")
}
else {
if let content = data {
do {
let json = try JSONSerialization.jsonObject(with: content, options: []) as? [String:Any] ?? [:]
if let ctattimetable = json["ctatt"] as? [String:Any] {
if let estArrivalTime = ctattimetable["eta"] as? [[String:Any]] {
for item in estArrivalTime {
if let headingTowards = item["destNm"] as? String,
let arrivalTime = item["arrT"] as? String {
let record = Destinations()
record.destination = headingTowards
record.time = arrivalTime
self.records.append(record)
}
self.tableView.reloadData()
}
}
}
}
catch { /* Handle error */ }
}
}
}
task.resume()
}
}
【问题讨论】:
标签: ios json uitableview swift3