ios - 将 “correct” 数据从 UITableViewController 中的选定单元格传递到 ViewController

标签 ios json swift uitableview

我遇到了同样的问题 here我还没有弄清楚如何将正确的数据从 TableViewController 传递到另一个 ViewController。我从 JSON 获取数据我拥有我需要的一切,但是通过选择显示的任何行数据是最后一行中的数据。我需要获取有关我选择 TableViewController 的确切行的信息。

import UIKit

class TableViewController: UITableViewController {

var TableData:Array< String > = Array < String >()

func getData(_ link: String) {
    let url: URL = URL(string: link)!
    let session = URLSession.shared
    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (data, response, error) in
        guard let _: Data = data , let _: URLResponse = response , error == nil else {
            return
        }
        self.extractJSON(data!)
    })
    task.resume()
}

func extractJSON(_ data: Data) {
    let json: Any?
    do {
        json = try JSONSerialization.jsonObject(with: data, options: [])
    } catch {
        return
    }
    guard let dataList = json as? NSArray else {
        return
    }
    if let countriesList = json as? NSArray {
        for i in 0 ..< dataList.count {
            if let countriesObj = countriesList[i] as? NSDictionary {
                if let countryName = countriesObj["country"] as? String {
                    if let countryCode = countriesObj["code"] as? String {
                        TableData.append(countryName + " [" + countryCode + "]")
                        UserDefaults.standard.set(String(describing: countryName), forKey: "name")
                        UserDefaults.standard.set(String(describing: countryCode), forKey: "code")
                        UserDefaults.standard.synchronize()
                    }
                }
            }
        }
    }

    DispatchQueue.main.async(execute: {self.doTableRefresh()})
}

func doTableRefresh() {
    self.tableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    getData("http://www.kaleidosblog.com/tutorial/tutorial.json")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return TableData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = TableData[indexPath.row]

    return cell
}
}

最佳答案

您可以使用tableViewDidSelectRowAtIndexPath委托(delegate)方法

let string = TableData[indexPath.row]

获取你的数据并将其传递给vc

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   let string = TableData[indexPath.row]

    let vc = // Your VC From Storyboard 
    vc.data =  string // 
    self.navigationController?.pushViewController(vc, animated: true)
}

关于ios - 将 “correct” 数据从 UITableViewController 中的选定单元格传递到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166116/

相关文章:

ios - 如何设置 NSExtensionActivationRule 谓词?

iOS:检测touchEnded是否来自滑动屏幕或抬起手指?

ios - WKWebView 没有将内容滚动到映射的 HTML

swift - Firebase 存储

ios - 使用 alamofire 下载不完整的文件

ios - 将 Storyboard项目的内容(包括约束)复制到 xib 文件

json - 使用 ARM 将 Azure RBAC 应用于资源

json - 在第一个文件后查找停止

javascript - jQuery UI 自动完成 url

swift - RAC3 中 MutableProperty 的多个观察者