title: How to create UITableViewCells with complex content in order to keep scrolling fluent
body: <p>I am working on a project where I have a table view which contains a number of cells with pretty complex content. It will be between usually not more than two, but in exceptions up to - lets say - 30 of them. Each of these complex cells contain a line chart. I am using ios-charts (<a href="https://github.com/danielgindi/ios-charts" rel="nofollow noreferrer">https://github.com/danielgindi/ios-charts</a>) for this.</p>
  
  <p>This is what the View Controller's content looks like:
  <img src="https://i.stack.imgur.com/eG0zT.png" alt="view controller content"></p>
  
  <p>The code I use for dequeuing the cells in the viewController's cellForRowAtIndexPath method is kind of the following:</p>
  
  <pre><code>var cell = tableView.dequeueReusableCellWithIdentifier("PerfgraphCell", forIndexPath: indexPath) as? UITableViewCell
  if cell == nil {
      let nib:Array = NSBundle.mainBundle().loadNibNamed("PerfgraphCell", owner: self, options: nil)
      cell = nib[0] as? FIBPerfgraphCell
  }
  
  cell.setupWithService(serviceObject, andPerfdataDatasourceId: indexPath.row - 1)
  return cell!
  </code></pre>
  
  <p>and in the cell's code I have a method "setupWithService" which pulls the datasources from an already existing object "serviceObject" and inits the drawing of the graph, like this:</p>
  
  <pre><code>func setupWithService(service: ServiceObjectWithDetails, andPerfdataDatasourceId id: Int) {
  
      let dataSource = service.perfdataDatasources[id]
      let metadata = service.perfdataMetadataForDatasource(dataSource)
  
      if metadata != nil {
          if let lineChartData = service.perfdataDatasourcesLineChartData[dataSource] {
  
              println("starting drawing for \(lineChartData.dataSets[0].yVals.count) values")
  
              chartView.data = lineChartData
          }
      }
  }
  </code></pre>
  
  <p>Now the problem: depending on how many values are to be drawn in the chart (between 100 and 2000) the drawing seems to get pretty complex. The user notices that when he scrolls down: as soon as a cell is to be dequeued that contains such a complex chart, the scrolling gets stuck for a short moment until the chart is initialized. That's of course ugly!</p>
  
  <p>For such a case, does it make sense to NOT dequeue the cells on demand but predefine them and hold them in an array once the data that is needed for graphing is received by the view controller and just pull the corresponding cell out of this array when it's needed? Or is there a way to make the initialization of the chart asynchronous, so that the cell is there immediately but the chart appears whenever it's "ready"?</p>
  
  <p>Thanks for your responses!</p>