Reputation: 121
I have a tableview loads data from php json using Alamofire, it loads perfect on the tableview, now I tried to pass the data to a second viewController to show more details, I faced this error which says Cannot find the data in scope
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
}
how can I pass the data from to the second view?
Upvotes: 4
Views: 264
Reputation: 1357
Ok
**
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)
}
**
If you want to pass your data from Controller1
to Controller2
.
In 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)
}
In Controller2
Create a Dictionary variable
var dictGetData:NSDictionary!
then print dictionary on view did load
override func viewDidLoad() {
super.viewDidLoad()
print(dictGetData as Any)
}
Upvotes: 0
Reputation: 1652
I'm not sure I understood what exactly is your problem. But if you need to access that data on the second view controller, the simplest way would be to inject that data into the DetailsViewController
you're instantiating when the cell is selected.
But first you need to create that property:
class DetailsViewController: UIViewController {
var data: JSON?
...
}
Then when instantiating that view controller:
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)
}
Now you can use that data on your DetailsViewController
to pull any information you need from the json response:
class DetailsViewController: UIViewController {
...
override viewDidLoad() {
super.viewDidLoad()
let title = data["title"]
...
}
}
Upvotes: 1
Reputation: 394
You pass your alamofire result to "data" variable. Therefore, you should use "data" while passing the value to another VC. In your case, it could be like;
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)
}
Upvotes: 0