i making quiz in tableview
has 4 buttons (options), tagged them on story board 201,202,203,204 , got of them in tableview
methods. after adding targets buttons, not able particular buttons in buttonclicked
method.
func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return questions.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell (cell.viewwithtag(100) as! uilabel).text = "q : " + (questions[indexpath.row].objectforkey("mocquestion")! as? string)! (cell.viewwithtag(100) as! uilabel).font = themefont (cell.viewwithtag(101) as! uilabel).text = questions[indexpath.row].objectforkey("op1")! as? string (cell.viewwithtag(102) as! uilabel).text = questions[indexpath.row].objectforkey("op2")! as? string (cell.viewwithtag(103) as! uilabel).text = questions[indexpath.row].objectforkey("op3")! as? string (cell.viewwithtag(104) as! uilabel).text = questions[indexpath.row].objectforkey("op4")! as? string let btn1 = (cell.viewwithtag(201) as! uibutton) let btn2 = (cell.viewwithtag(202) as! uibutton) let btn3 = (cell.viewwithtag(203) as! uibutton) let btn4 = (cell.viewwithtag(204) as! uibutton) // btn1.tag = indexpath.row * 100 + 0 // btn1.tag = indexpath.row * 100 + 1 // btn1.tag = indexpath.row * 100 + 2 // btn1.tag = indexpath.row * 100 + 3 btn1.addtarget(self, action: #selector(quiz.buttonclicked(_:)),forcontrolevents: uicontrolevents.touchupinside) btn2.addtarget(self, action: #selector(quiz.buttonclicked(_:)),forcontrolevents: uicontrolevents.touchupinside) btn3.addtarget(self, action: #selector(quiz.buttonclicked(_:)),forcontrolevents: uicontrolevents.touchupinside) btn4.addtarget(self, action: #selector(quiz.buttonclicked(_:)),forcontrolevents: uicontrolevents.touchupinside) return cell } func buttonclicked(sender:uibutton) { let tag = sender.tag print(tag) }
if want indexpath
access questions array
can try this.
func buttonclicked(sender:uibutton) { let center = sender.center let point = sender.superview!.convertpoint(center, toview:self.tableview) let indexpath = self.tableview.indexpathforrowatpoint(point) //now have tag of button check if (sender.tag == 201) { print("option a") } else if (sender.tag == 202) { print("option b") } else if (sender.tag == 203) { print("option c") } else { print("option d") } print(question[indexpath.row]) }
Comments
Post a Comment