【问题标题】:How to get indexPath match I click send data with segue如何获得 indexPath 匹配我单击使用 segue 发送数据
【发布时间】:2016-06-02 09:26:58
【问题描述】:

我希望当我点击UITableView 行时 它将发送一个 ID 到下一个 UIView 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
        }
    }
}
}

【问题讨论】:

  • 错误是什么?

标签: json swift uitableview segue


【解决方案1】:
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 = ""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 2018-12-19
    • 2014-11-23
    • 2014-12-30
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多