ios - Perform action button in uitableview cell to perform delete functionality -
i have 1 table view, , in each cell have x
button delete particular cell . have done iboutlet of button custom table view cell. , in cell row @ index path have done add target button. when press button in table view cell. app crash. if put braekpoint. showing in target added code line.
here code :
func followbutton(sender: anyobject) { if let button = sender as? uibutton { print("button clicked: \(button.tag)") let item = addtocartdata[button.tag] print("name: \(item.cartid!)") let headers = [ "cache-control": "no-cache", "postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec" ] let jsonobj:dictionary<string, any> = [ "cartid" : "\(item.cartid!)", "carttype" : "3" ] if (!jsonserialization.isvalidjsonobject(jsonobj)) { print("is not valid json object") return } if let postdata = try? jsonserialization.data(withjsonobject: jsonobj, options: jsonserialization.writingoptions.prettyprinted) { let request = nsmutableurlrequest(url: nsurl(string: "http://exp.cart.php")! url, cachepolicy: .useprotocolcachepolicy,timeoutinterval: 10.0) request.httpmethod = "post" request.allhttpheaderfields = headers request.httpbody = postdata let session = urlsession.shared let datatask = session.datatask(with: request urlrequest, completionhandler: { (data, response, error) -> void in if (error != nil) { ///print(error) } else { dispatchqueue.main.async(execute: { if let json = (try? jsonserialization.jsonobject(with: data!, options: [])) as? dictionary<string,anyobject> { let status = json["status"] as? int; if(status == 1) { // self.tableview.reloaddata() print("items deleted") self.tableview.reloaddata() } } }) } }) datatask.resume() } } }
in swift 3, action
's argument selector, syntax #selector(method-signature)
. final code is:
func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cartcel", for: indexpath) as! carttableviewcell cell.deletebutton.tag = indexpath.row; cell.deletebutton.addtarget(self, action: #selector(followbutton(sender:)), for: .touchupinside) return cell; }
for additional question:
i need value of each cell , need pass parameter 1 api call. if not mach. how can selector. perform delete functionality
func followbutton(sender: anyobject) { if let button = sender as? uibutton { print("button clicked: \(button.tag)") let item = addtocartdata[button.tag] print("name: \(item.cartproname), quality: \(item.proqty), price: \(item.cartproprice)") } }
Comments
Post a Comment