ios - Loading UITableViewCells that are not in display -


i'm having issue uitableviewcells not loading unless scroll down reveal them, problem each custom cell has toggle switch in them , if it's on add cell's value calculation, calculation incomplete since missing of cells did not load (and load once scroll down).
makes sense. on bluetooth connection app, , it's supposed refresh table every time receives new values. here's code load cells:

//table loading / reloading functions: func numberofsections(in tableview: uitableview) -> int {     return 1  }  func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{         return temperaturereadingarray.count //should return 1 or 9 depending on array received }  func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {      let cell = tableview.dequeuereusablecell(withidentifier: temperaturereading) as! temperaturereading      cell.channellabel.text = channelnames[indexpath.row]     cell.readinglabel.text = temperaturereadingarray[indexpath.row]      if (channelnames.count == 1)     {      cell.toggleswitch.ishidden = true     }     if (cell.toggleswitch.ison)     {         if let value = double(cell.readinglabel.text!)         {             print("valid number: \(value), appending")             tnuarray.append(value)         }         else         {             print("not valid number reading")         }     }     return cell } 

as can see, check toggle switch , far code works intended, assuming cells fit inside visible window, when run on device smaller screen or if many values (more fit in screen) cells not visible not counted towards tnu calculations:

func updateincomingdata () {      print ("received string:")     print (receiveddatastring)     print("clearing tnu array")     tnuarray.removeall()     temperaturereadingarray = receiveddatastring.components(separatedby: " ")     self.temperaturereadingstable.reloaddata()     dispatchqueue.main.async {         self.calculatetnu() //need dispatch queue run after finished reloading data     } }  func calculatetnu() {     var tnu: double = 0.000      let minvalue = tnuarray.min()     let maxvalue = tnuarray.max()     print(tnuarray)     if (minvalue != nil && maxvalue != nil)     {         tnu = maxvalue! - minvalue!         calculatedtnu = string(format:"%.3f", tnu)         tnulabel.text = calculatedtnu     }     else     {         print("max or min had nil values")         tnulabel.text = "na"     } } 

i hope being clear, try summarize in sequence of things supposed happen:
- receive bluetooth string (it contains multiple values separated space)
- parse values array of values, lets assume 9 values
- add 1 value per cell, toggle switch
- check if cell's switch has been toggled , if has add value array calculate max difference between values
- calculate max-min value
- display value.

the problem calculating max-min value since array uses calculate requires cells load, , cells don't fit screen don't load unless scroll.
note: i've tried using dispatchqueue.main.async , reloaddata(), doesn't solve issue, since doesn't load cells not in display

simply use data model class , initialize class values populated in uitableview.

suppose if cells have toggle switches initialize model class bool values (different properties different cells) , set them default value (yes/no)

and in cellforrowatindexpath method

if(modelclass.togglevalue)   cell.toggleswitch.seton(true) else   cell.toggleswitch.seton(false) 

and whenever toggle switch, update model class respected value. , done. make calculation on values stored in model class. not cell directly.

hope you..!


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -