快速将数据从 tableview 传递到 viewContoller [英] passing data from tableview to viewContoller in swift

查看:31
本文介绍了快速将数据从 tableview 传递到 viewContoller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tableview 使用 Alamofire 从 php json 加载数据,它在 tableview 上加载完美,现在我尝试将数据传递给第二个 viewController 以显示更多详细信息,我遇到了这个错误,上面写着找不到范围内的数据

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?

推荐答案

我不确定我是否理解您的问题到底是什么.但是,如果您需要访问第二个视图控制器上的数据,最简单的方法是将该数据注入到您在选择单元格时实例化的 DetailsViewController 中.
但首先您需要创建该属性:

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)
    }

现在您可以使用 DetailsViewController 上的数据从 json 响应中提取您需要的任何信息:

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

}

这篇关于快速将数据从 tableview 传递到 viewContoller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆