ボタンにセレクタを提供し、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
}
次に、次のビューコントロールにあなたのセグを実行します。
なぜあなたの関数に未使用の関数がありますか? – Moritz
product_idとproduct_id_asは、このメソッド以外のクラスで宣言された変数のようです。だから、おそらくそれらは他の方法で修正されるでしょう。メソッド内でローカルに宣言して、何かが変更されているかどうか確認してください。 – Gustanas
次のVCにどのようにナビゲートしていますか?そのコードも投稿してください。そして何がrenewQuestionsがcellForRow内部で何をしているのかを調べることができます... –