在 swift 中将数据从 tableview 传递到 viewController
passing data from tableview to viewContoller in swift
我有一个表视图使用 Alamofire 从 php json 加载数据,它在表视图上加载完美,现在我试图将数据传递给第二个 viewController 以显示更多详细信息,我遇到了这个错误,它说无法在范围
中找到数据
func getUsers() {
AF.request(SiteUrl).validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let jsonData = try JSON(data: json)
self.data = jsonData.arrayValue
self.tableView.reloadData() // we are already on the main thread
//print("DATA PARSED: \(jsonData)")
}
catch {
print("JSON Error", error)
}
}
case .failure(let error):
print(error)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
let item = data[indexPath.row]
customCell.AdTitle?.text = item["title"].string
customCell.AdDate?.text = item["time"].string
let imageUrl = item["document"].string
let url = NSURL(string:("https://qateef-ads.co/uploads/" + imageUrl!))
customCell.AdImage.sd_setImage(with: url as URL?, placeholderImage: UIImage(named: "placeholder.png"))
return customCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.imageView = UIImage(named: url)
vc?.AdTitle = item["title"].string
}
如何将数据从中传递到第二个视图?
我不确定我是否理解您的问题到底是什么。但是如果您需要在第二个视图控制器上访问该数据,最简单的方法是将该数据注入到您在选择单元格时实例化的 DetailsViewController
。
但首先你需要创建 属性:
class DetailsViewController: UIViewController {
var data: JSON?
...
}
然后在实例化该视图控制器时:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController else { return }
vc.data = data[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
现在您可以使用 DetailsViewController
上的数据从 json 响应中提取您需要的任何信息:
class DetailsViewController: UIViewController {
...
override viewDidLoad() {
super.viewDidLoad()
let title = data["title"]
...
}
}
您将 alamofire 结果传递给“数据”变量。因此,您应该在将值传递给另一个 VC 时使用“数据”。在你的情况下,它可能是这样的;
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
vc?.imageView = UIImage(named: url)
let item = data[indexPath.row]
vc?.AdTitle = item["title"].string
self.navigationController?.pushViewController(vc!, animated: true)
}
好的
**
if let json = response.data {
let JSON22 = json as! NSDictionary
do{
let jsonData = try JSON(data: JSON22)
self.data = jsonData.arrayValue
self.tableView.reloadData() // we are already on the main thread
//print("DATA PARSED: \(jsonData)")
}
catch {
print("JSON Error", error)
}
**
如果您想将数据从 Controller1
传递到 Controller2
。
在Controller1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = data[indexPath.row]
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
vc!.dictGetData = item
self.navigationController?.pushViewController(vc!, animated: true)
}
在Controller2
创建字典变量
var dictGetData:NSDictionary!
然后在视图上打印字典并加载
override func viewDidLoad() {
super.viewDidLoad()
print(dictGetData as Any)
}
我有一个表视图使用 Alamofire 从 php json 加载数据,它在表视图上加载完美,现在我试图将数据传递给第二个 viewController 以显示更多详细信息,我遇到了这个错误,它说无法在范围
中找到数据func getUsers() {
AF.request(SiteUrl).validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let jsonData = try JSON(data: json)
self.data = jsonData.arrayValue
self.tableView.reloadData() // we are already on the main thread
//print("DATA PARSED: \(jsonData)")
}
catch {
print("JSON Error", error)
}
}
case .failure(let error):
print(error)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
let item = data[indexPath.row]
customCell.AdTitle?.text = item["title"].string
customCell.AdDate?.text = item["time"].string
let imageUrl = item["document"].string
let url = NSURL(string:("https://qateef-ads.co/uploads/" + imageUrl!))
customCell.AdImage.sd_setImage(with: url as URL?, placeholderImage: UIImage(named: "placeholder.png"))
return customCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.imageView = UIImage(named: url)
vc?.AdTitle = item["title"].string
}
如何将数据从中传递到第二个视图?
我不确定我是否理解您的问题到底是什么。但是如果您需要在第二个视图控制器上访问该数据,最简单的方法是将该数据注入到您在选择单元格时实例化的 DetailsViewController
。
但首先你需要创建 属性:
class DetailsViewController: UIViewController {
var data: JSON?
...
}
然后在实例化该视图控制器时:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController else { return }
vc.data = data[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
现在您可以使用 DetailsViewController
上的数据从 json 响应中提取您需要的任何信息:
class DetailsViewController: UIViewController {
...
override viewDidLoad() {
super.viewDidLoad()
let title = data["title"]
...
}
}
您将 alamofire 结果传递给“数据”变量。因此,您应该在将值传递给另一个 VC 时使用“数据”。在你的情况下,它可能是这样的;
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
vc?.imageView = UIImage(named: url)
let item = data[indexPath.row]
vc?.AdTitle = item["title"].string
self.navigationController?.pushViewController(vc!, animated: true)
}
好的
**
if let json = response.data {
let JSON22 = json as! NSDictionary
do{
let jsonData = try JSON(data: JSON22)
self.data = jsonData.arrayValue
self.tableView.reloadData() // we are already on the main thread
//print("DATA PARSED: \(jsonData)")
}
catch {
print("JSON Error", error)
}
**
如果您想将数据从 Controller1
传递到 Controller2
。
在Controller1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = data[indexPath.row]
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
vc!.dictGetData = item
self.navigationController?.pushViewController(vc!, animated: true)
}
在Controller2
创建字典变量
var dictGetData:NSDictionary!
然后在视图上打印字典并加载
override func viewDidLoad() {
super.viewDidLoad()
print(dictGetData as Any)
}