Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

ios - passing data from tableview to viewContoller in swift

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"]
        ...
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...