将 UITableViewController 中选定单元格的“正确"数据传递给 ViewController [英] Passing the “correct” Data from the selected cell in a UITableViewController to a ViewController
本文介绍了将 UITableViewController 中选定单元格的“正确"数据传递给 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了同样的问题 这里 我还没有想出如何将正确的数据从 TableViewController 传递到另一个 ViewController.我从 JSON 获取数据我拥有我需要的一切,但是通过选择任何行数据显示的是最后一行中的数据.我需要获取有关我选择 TableViewController 的确切行的信息.
I'm having the very same trouble here I have not figured out yet how to pass the correct data from TableViewController to another ViewController. I get data from JSON I have everything I need but by selecting any row data showing is the data in the last row. I need to get the information about the exact row I select the 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
委托方法
let string = TableData[indexPath.row]
获取您的数据并将其传递给 vc
to get your data and pass it to 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)
}
这篇关于将 UITableViewController 中选定单元格的“正确"数据传递给 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文