【问题标题】:Swift 3 TableView Segue to Multiple Other TableViewsSwift 3 TableView Segue 到多个其他 TableViews
【发布时间】: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


    【解决方案1】:

    基本上,你已经自己想通了:)。

    经典的方法是使用带有表视图控制器 #1 的导航控制器作为其根视图控制器,该控制器显示火车线路列表。配置一个原型单元以显示有关火车线路的一般信息。此外,连接此单元格的选择序列以显示表视图控制器 #2。

    表视图控制器 #2 可以有一个原型单元格,显示有关目的地的信息(名称和到达时间)。

    在表视图控制器#1 的prepare(for:sender:) 中,只需遵循代码模板的cmets:segue.destinationViewController 将是即将显示的表视图控制器#2。如此检索它并使用有关所选火车线路的详细信息(所选火车线路的目的地列表)对其进行配置。

    或者“我不确定这是否有效”是什么意思?

    无论如何,您可以考虑的替代方案(例如,取决于您所针对的屏幕尺寸)是使用拆分视图控制器或单个表视图控制器,当您点击火车线路时,该控制器会展开以内联​​显示目的地。

    希望这能让你走上正轨,可以这么说:)。

    【讨论】:

    • 谢谢!这对弄清楚大局很有帮助。另外,感谢您考虑的替代方案和火车双关语:)
    • 不客气,很高兴我能帮上忙。也感谢您的接受和投票。考虑到这个话题,这个笑话就摆在了盘子上:)。
    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多