ios - Programmatically added buttons target action not working in custom tableviewcell -
i added programmatically 2 buttons in custom tableviewcell, target action not working. can solve problem ?(swift)
this code of adding buttons:
override func awakefromnib() { super.awakefromnib() func createcuebutton() -> uibutton{ let button = uibutton() button.titlelabel?.textcolor = uicolor.whitecolor() button.titlelabel?.font = uifont.boldsystemfontofsize(16.0) return button } completebutton = createcuebutton() completebutton.settitle("complete", forstate: uicontrolstate.normal) completebutton.backgroundcolor = uicolor.greencolor() completebutton.addtarget(self, action: "buttonpressed:", forcontrolevents: .touchupinside) addsubview(completebutton) deletebutton = createcuebutton() deletebutton.settitle("delete", forstate: .normal) deletebutton.backgroundcolor = uicolor.redcolor() deletebutton.addtarget(self, action: "buttonpressed:", forcontrolevents: .touchupinside) addsubview(deletebutton) } func buttonpressed(sender: uibutton){ var alertview = uialertview(); alertview.addbuttonwithtitle("ok"); alertview.title = "alert"; alertview.message = "button pressed!!!"; alertview.show(); } override func layoutsubviews() { super.layoutsubviews() completebutton.frame = cgrect(x: 0, y: 0, width: cueswidth, height: bounds.size.height) deletebutton.frame = cgrect(x: bounds.size.width - cueswidth, y: 0, width: cueswidth, height: bounds.size.height) }
i preferred answer question, please follow below steps
func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { return 75 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return 1 } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let identifier = "custom" var cell: customcell! = tableview.dequeuereusablecellwithidentifier(identifier) as? customcell if cell == nil { tableviewcustom.registernib(uinib(nibname: "customcell", bundle: nil), forcellreuseidentifier: identifier) cell = tableviewcustom.dequeuereusablecellwithidentifier(identifier) as? customcell } //first button adding in custom cell let btn = uibutton.buttonwithtype(uibuttontype.custom) uibutton btn.backgroundcolor = uicolor.greencolor() btn.settitle("complete", forstate: uicontrolstate.normal) btn.frame = cgrectmake(0, 6, 80, 40) btn.addtarget(self, action: "completebuttontouched:", forcontrolevents: uicontrolevents.touchupinside) cell.contentview.addsubview(btn) //second button adding in customcell let btn2 = uibutton.buttonwithtype(uibuttontype.custom) uibutton btn2.backgroundcolor = uicolor.greencolor() btn2.settitle("delete", forstate: uicontrolstate.normal) btn2.frame = cgrectmake(180, 5, 80, 40) btn2.addtarget(self, action: "deletebuttontouched:", forcontrolevents: uicontrolevents.touchupinside) cell.contentview.addsubview(btn2) return cell } func completebuttontouched(sender:uibutton!) { println("complete button target action works!!!") } func deletebuttontouched(sender:uibutton!) { println("delete button target action works!!!") }
whatever when add button custom cell programmatically, please apply below code
cell.contentview.addsubview(btn)
Comments
Post a Comment