如何获得indexPath匹配我点击发送数据与segue

时间:2016-06-02 09:26:58

标签: json swift uitableview segue

我希望当我点击UITableView行时 它会向下一个UIView发送一个ID ID获取表单json数据 我无法获得UITableView indexPath 有一些错误。 我该如何解决这个问题?

这是我的代码

func get(){
    let url = NSURL(string: "http://test.php?id=01")
    let data = NSData(contentsOfURL: url!)
    values = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
    tableView1.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
    let maindata = values[indexPath.row]
    cell.name.text = maindata["product"] as? String
    cell.vintage.text = maindata["vintage"] as? String

    ID = maindata["ID"] as? String


    return cell;
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
    if segue.identifier == "showWineDetail"{
        if indexPath = self.tableView1.indexPathForSelectedRow{
            let destination = segue.destinationViewController as! ViewController
            destination = self.ID
        }
    }
}
}

1 个答案:

答案 0 :(得分:3)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
    let maindata = values[indexPath.row]
    cell.name.text = maindata["product"] as? String
    cell.vintage.text = maindata["vintage"] as? String

    return cell;
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
    if segue.identifier == "showWineDetail"{
        if let indexPath = self.tableView1.indexPathForSelectedRow{
            let destination = segue.destinationViewController as! ViewController
            let maindata = values[indexPath.row]
            destination.ID = maindata["ID"] as? String
        }
    }
}

在目标中创建(ViewController)var ID:String =“”