2016-05-02 7 views
0

私はSwiftを初めて使用しています。URLに変数を渡す際に問題が発生しています。 アイデアは、ボタンをクリックし、製品IDを渡し、次のView Controllerで、特定の製品IDに関連する情報を表示することです。Swiftが正しい変数値を渡さない

これは、クリックしたIDではなく、最後のIDを渡し続けます。

私はprint(product_id_as)を使ってIDが正しく検索されているかどうかを確認しましたが、問題はありません。

私は外部データベースからJSONデータを表示するためにテーブルビューを使用しています。 これはデータを表示するために使用しているコードです。

+1

なぜあなたの関数に未使用の関数がありますか? – Moritz

+1

product_idとproduct_id_asは、このメソッド以外のクラスで宣言された変数のようです。だから、おそらくそれらは他の方法で修正されるでしょう。メソッド内でローカルに宣言して、何かが変更されているかどうか確認してください。 – Gustanas

+0

次のVCにどのようにナビゲートしていますか?そのコードも投稿してください。そして何がrenewQuestionsがcellForRow内部で何をしているのかを調べることができます... –

答えて

0

ボタンにセレクタを提供し、indexPath.rowをbuttonsタグとして割り当てます。 RenewQuestionsがボタンのセレクタになります。これを移動してタグを使用してボタンをクリックするとデータが抽出されます

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell 
let maindata = values[indexPath.row] 
cell.product.text = maindata["product"] as? String 
product_id = (maindata["id"] as? String)! 
product_id_as = Int(product_id)! 
buttonTitle = (maindata["product_id"] as? String)! 
cell.testButton.setTitle(buttonTitle, forState: .Normal) 
cell.testButton.addTarget(self, action:#selector(ViewController.renewQuestions(_:)), forControlEvents:UIControlEvents.TouchUpInside) 
cell.testButton.tag = indexPath.row 
return cell; 
} 


func renewQuestions(sender : UIButton) { 
let selectedItem = sender.tag 
let selectedData = values[selectedItem] 
let selectedProduct_id = (selectedData["id"] as? String)! 
let selectedProduct_id_as = Int(selectedProduct_id)! 
let url = NSURL(string: "http://initiateaustralia.com.au/initiate/vq.php?product=\(selectedProduct_id_as))") 
let data = NSData(contentsOfURL: url!) values = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray 
} 

次に、次のビューコントロールにあなたのセグを実行します。

関連する問題