ios - how to pass the data from table view cell class to view controller table view class? -
in had 3 sections in need last section tableviewcell data needs pass table view class thinking use nsnotificationcenter pass data had seen lots of answers not able understand how pass can me how post data using nsnotification
table view class
override func viewdidload() { super.viewdidload() _ = uiapplication.shared.statusbarorientation tabledetails.ishidden = true continuebutton.layer.cornerradius = 5 myactivityindicator.frame = cgrect(x: 130, y: 320, width: 30, height: 30) myactivityindicator.hideswhenstopped = true myactivityindicator.startanimating() view.addsubview(myactivityindicator) myactivityindicator.translatesautoresizingmaskintoconstraints = false let horizontalconstraint = nslayoutconstraint(item: myactivityindicator, attribute: nslayoutattribute.centerx, relatedby: nslayoutrelation.equal, toitem: view, attribute: nslayoutattribute.centerx, multiplier: 1, constant: 0) view.addconstraint(horizontalconstraint) let verticalconstraint = nslayoutconstraint(item: myactivityindicator, attribute: nslayoutattribute.centery, relatedby: nslayoutrelation.equal, toitem: view, attribute: nslayoutattribute.centery, multiplier: 1, constant: 0) view.addconstraint(verticalconstraint) self.shippingaddressurl() tabledetails.delegate = self tabledetails.datasource = self tabledetails.separatorinset = uiedgeinsets.zero tabledetails.rowheight = uitableviewautomaticdimension tabledetails.estimatedrowheight = 50 self.title = "checkout" // additional setup after loading view. } func shippingaddressurl() { let url = nsurl(string: self.url) urlsession.shared.datatask(with: (url url?)!, completionhandler: {(data, response, error) -> void in if let jsonobj = try? jsonserialization.jsonobject(with: data!, options: .allowfragments) as? nsdictionary { self.shippingarray = (jsonobj!.value(forkey: "address") as? [[string: anyobject]])! operationqueue.main.addoperation({ self.tabledetails.reloaddata() }) } }).resume() } @ibaction func selectradiobutton(_ sender: kgradiobutton) { let chekindex = self.checkisradioselect.index(of: sender.tag) _ = self.checkisbuttonenable.index(of: sender.tag) if sender.isselected { } else{ if(chekindex == nil){ self.checkisradioselect.removeall(keepingcapacity: false) self.checkisradioselect.append(sender.tag) self.checkisbuttonenable.removeall(keepingcapacity: false) self.checkisbuttonenable.append(sender.tag) self.tabledetails.reloaddata() } } } func numberofsections(in tableview: uitableview) -> int{ return 3 } func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? { if (section == 0){ return "shipping address" } else if (section == 2){ return "shipping method" } else{ return "" } } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat{ if (indexpath.section == 0){ return uitableviewautomaticdimension } else if (indexpath.section == 1){ return 62 } else { return 282 } } func tableview(_ tableview: uitableview, willdisplayheaderview view: uiview, forsection section: int){ let header = view as! uitableviewheaderfooterview header.textlabel?.textcolor = uicolor.gray header.textlabel?.textalignment = nstextalignment.center header.textlabel?.font = uifont(name: "futura", size: 17) } @ibaction func newaddressaction(_ sender: any) { let storyboard = uistoryboard(name: "main", bundle: nil) let addtocartvc = storyboard.instantiateviewcontroller(withidentifier: "newaddress") as! newaddressviewcontroller self.navigationcontroller?.pushviewcontroller(addtocartvc, animated: true) } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{ if (section == 0){ return shippingarray.count } else { return 1 } } @ibaction func continuebuttonaction(_ sender: any) { if selected == false{ let radiobutton = sclalertview() _ = radiobutton.showerror("warning", subtitle: "please select shipping method", closebuttontitle: "ok") }else{ let storyboard = uistoryboard(name: "main", bundle: nil) let addtocartvc = storyboard.instantiateviewcontroller(withidentifier: "payment") as! paymentmethodviewcontroller self.navigationcontroller?.pushviewcontroller(addtocartvc, animated: true) } } @ibaction func deleteaction(_ sender: uibutton) { shippingarray.remove(at:sender.tag) self.tabledetails.reloaddata() } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { if (indexpath.section == 0) { let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! addresstableviewcell tabledetails.ishidden = false myactivityindicator.stopanimating() let arr = shippingarray[indexpath.row] cell.deletebutton.tag = indexpath.row cell.namelabel.text = arr["name"] as? string cell.addresslabel.text = arr["address"]as? string let mobilenumber : = arr["number"] anyobject cell.mobilenumberlabel.text = "\(mobilenumber)" cell.radiobutton.tag = indexpath.row cell.editbutton.tag = indexpath.row cell.deletebutton.tag = indexpath.row cell.editbutton.ishidden = true cell.deletebutton.ishidden = true let checkindex = self.checkisradioselect.index(of: indexpath.row) if(checkindex != nil){ cell.radiobutton.isselected = true cell.editbutton.ishidden = false cell.deletebutton.ishidden = false }else { cell.radiobutton.isselected = false cell.editbutton.ishidden = true cell.deletebutton.ishidden = true } return cell } else if (indexpath.section == 1){ let cell = tableview.dequeuereusablecell(withidentifier: "addresscell", for: indexpath) as! createnewaddresstableviewcell cell.newaddressbutton.addtarget(self, action: #selector(newaddressaction(_:)), for: .touchupinside) return cell } else { let cell = tableview.dequeuereusablecell(withidentifier: "shippingmethodcell", for: indexpath) as! methodtableviewcell return cell } }
table view cell class
var chekindex:indexpath? var arrayss = [string:anyobject]() var keys = [string]() let urlstring = "http://www.json-generator.com/api/json/get/bvgbyvqgmq?indent=2" override func awakefromnib() { super.awakefromnib() self.shippingmethodurl() shippingtableview.delegate = self shippingtableview.datasource = self shippingtableview.rowheight = uitableviewautomaticdimension shippingtableview.estimatedrowheight = shippingtableview.rowheight // initialization code } @ibaction func paymentradioaction(_ sender: kgradiobutton) { _ = sender.center let centralpoint = sender.superview?.convert(sender.center, to:self.shippingtableview) let indexpath = self.shippingtableview.indexpathforrow(at: centralpoint!) if sender.isselected { } else{ chekindex = indexpath isselected = true self.shippingtableview.reloaddata() } } func shippingmethodurl() { let url = nsurl(string: self.urlstring) urlsession.shared.datatask(with: (url url?)!, completionhandler: {(data, response, error) -> void in if let jsonobj = try? jsonserialization.jsonobject(with: data!, options: .allowfragments) as? nsdictionary { self.arrayss = jsonobj as! [string : anyobject] print(self.arrayss) self.keys = jsonobj?.allkeys as! [string] print(self.keys) operationqueue.main.addoperation({ self.shippingtableview.reloaddata() let sectionheight = self.arrayss.count * 31 let cellheight = self.keys.count * 44 self.shippingheightconstraint.constant = cgfloat(sectionheight + cellheight) }) } }).resume() } func numberofsections(in tableview: uitableview) -> int{ return arrayss.count } func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? { return self.keys[section] } func tableview(_ tableview: uitableview, willdisplayheaderview view: uiview, forsection section: int){ let header = view as! uitableviewheaderfooterview header.tintcolor = uicolor.white header.textlabel?.textcolor = uicolor.darkgray header.textlabel?.textalignment = nstextalignment.left header.textlabel?.font = uifont(name: "futura", size: 17) } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{ let key = self.keys[section] let :[any] = arrayss[key] as! [any] return a.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "shippingcell", for: indexpath) as! shippingmethodtableviewcell let bgcolorview = uiview() bgcolorview.backgroundcolor = uicolor.white cell.selectedbackgroundview = bgcolorview let key = self.keys[indexpath.section] var :[any] = arrayss[key] as! [any] var dictionary = a[indexpath.row] as! [string:any] let name = dictionary["name"] let price = dictionary ["price"] cell.methodlabel.text = name as? string cell.pricelabel.text = price as? string cell.radiobutton.addtarget(self, action: #selector(paymentradioaction), for: .touchupinside) if chekindex == indexpath { cell.radiobutton.isselected = true } else { cell.radiobutton.isselected = false } return cell }
instead of creating ibaction in uitableviewcell class, addtarget button in viewcontroller
class in cellforrow
method:-
cell.radiobutton.addtarget(self, action: #selector(yourviewcontroller.somemethod(_sender:)), for: uicontrolevents.touchupinside)
and call api in viewcontroller itself, wont have pass data:-
func somemethod(_sender: uibutton) { // call api here in view controller class }
Comments
Post a Comment