iOS如何将单元格数据发送到另一个VC使用解析Swift

时间:2018-05-17 05:41:14

标签: ios swift uitableview

如何在点击单元格时将数据从我的表格视图单元格发送到另一个VC。

我从DB获取数据的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! cellRequestsTVC

    let good = data[indexPath.row]
    name = good["username"] as! String
    cell.userNameLable.text = "@\(name)"

    area = good["place"] as! String
    cell.areaLable.text = "\(area)"

    cell.descriptionLable.text = good["description"] as? String
    cell.priorityLable.text = "Priority " + ((good["priority"] as? Int)?.description)!

    let imageProblems = good["image"] as? PFFile
    imageProblems?.getDataInBackground{ (imageData, error)in
        if imageData != nil {
            let image = UIImage(data: imageData!)
            cell.problemImage.image = image
        }
    }
    return cell
}

它完美无瑕。但现在我的目标是从另一个VC中的单元格打开数据。 例如:

Example

1 个答案:

答案 0 :(得分:3)

您必须实施 UITableViewDelegate

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let good = data[indexPath.row]
// Here you can either perform a segue or push view controller to UINavigationController

//Push View controller 

    let storyBoard : UIStoryboard =  UIStoryboard(name: "Main", bundle: nil)
    let vc : DetailViewController = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    vc.sharedData = data // here you pass data
    self.navigationController?.pushViewController(vc, animated: true)

}

DetailViewController创建具有所需类型的数据对象

var sharedData : [String : Any]! // Take optional variable if object can be nil & here assuming data contains object of type [String : Any]

您可以在此处查看文档 UITableViewDelegate