ios - In table view cell data was not loading in screen during it's launch? -
in having 3 sections in table view in first section have addresses , radio buttons if click on radio button active , particular address posting depending on address selection third section needs call api , load data in second table view present in third section here problem during loading first time when app launched in simulator not loading third section cell data can 1 me how reduce error ?
here code table view class
func numberofsections(in tableview: uitableview) -> int { if ((addressselected == true || checkispaymentradioselect == true) && selected == false) { return 3 }else { return 2 } } func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? { if ((addressselected == true || checkispaymentradioselect == true) && selected == false) { if (section == 0) { return "shipping address" } else if (section == 2) { return "shipping method" } else { return "" } } else { if (section == 0) { return "shipping address" } else{ return "" } } } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{ if (section == 0) { return shippingarray.count } else { return 1 } } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat{ if ((addressselected == true || checkispaymentradioselect == true) && selected == false){ if (indexpath.section == 0){ return uitableviewautomaticdimension } else if (indexpath.section == 1){ return 62 } else { print(height) return cgfloat(height) } } else{ if (indexpath.section == 0){ return uitableviewautomaticdimension } else if (indexpath.section == 1){ return 62 } else { return 0 } } } 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 activityindicator.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 cell.radiobutton.addtarget(self, action: #selector(selectradiobutton(_:)), for: .touchupinside) cell.deletebutton.addtarget(self, action: #selector(deleteaction(button:)), for: .touchupinside) 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 } if (checkispaymentradioselect == true){ let defaultvalue = arr["default"] as! int if defaultvalue == 1 { cell.radiobutton.isselected = true cell.editbutton.ishidden = false cell.deletebutton.ishidden = false addressselected = true tabledetails.tablefooterview?.ishidden = false } } 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 cell.delegate = self cell.booldelegate = self cell.shippingtableview.reloaddata() if shippingradio == true { cell.select = shippingradio cell.boolselected() cell.shippingmethodurl() cell.shippingtableview.reloaddata() } else{ cell.select = methodradio cell.shippingtableview.reloaddata() } return cell } }
in cell class had got api data , passed table view shown in code need call api during cell selection of address can me how clear error or alternative this
var chekindex:indexpath? var arrayss = [string:any]() var keys = [string]() let urlstring = "http://www.json-generator.com/api/json/get/bvgbyvqgmq?indent=2" var delegate: checkoutdelegate? var heightconstant: int? var name = [string]() var totalcount = 0 var radioselected:bool? var radioselection: bool? var booldelegate: boolvalidationdelegate? var select:bool? override func awakefromnib() { super.awakefromnib() radioselection = false self.shippingmethodurl() shippingtableview.delegate = self shippingtableview.datasource = self shippingtableview.rowheight = uitableviewautomaticdimension shippingtableview.estimatedrowheight = shippingtableview.rowheight // initialization code } func paymentradioaction(button : kgradiobutton) { _ = button.center let centralpoint = button.superview?.convert(button.center, to:self.shippingtableview) let indexpath = self.shippingtableview.indexpathforrow(at: centralpoint!) if button.isselected { } else{ chekindex = indexpath radioselection = true self.shippingtableview.reloaddata() self.booldelegate?.boolvalidation(bool: radioselection!) } } func shippingmethodurl() { guard let url = url(string: self.urlstring) else {return} urlsession.shared.datatask(with: url, completionhandler: {(data, response, error) -> void in if let data = data, let jsonobj = (try? jsonserialization.jsonobject(with: data, options: .allowfragments)) as? [string:any] { self.arrayss = jsonobj self.keys = array(jsonobj.keys) value in jsonobj.values { if let array = value as? [[string:any]] { element in array { if (element["name"] as? string) != nil { self.totalcount += 1 } } } } dispatchqueue.main.async { self.shippingtableview.reloaddata() let sectionheight = self.arrayss.count * 31 let cellheight = self.totalcount * 44 self.shippingheightconstraint.constant = cgfloat(sectionheight + cellheight) self.heightconstant = int(self.shippingheightconstraint.constant) self.delegate?.heightconstant(int: self.heightconstant!) } } }).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, 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 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(button:)), for: .touchupinside) if chekindex == indexpath { cell.radiobutton.isselected = true } else { cell.radiobutton.isselected = false } return cell }
and first time image loading shown below enter image description here , if select radio button in first section working fine expected , image shown below enter image description here
Comments
Post a Comment