title: UITableView with autolayout not smooth when scrolling
body: <p>I'm using XIB files to design cells in my UITableView. I also use the dequeue mecanism, for instance : <code>let cell = tableView.dequeueReusableCellWithIdentifier("articleCell", forIndexPath: indexPath) as! ArticleTableViewCell</code>. I precalculate all my row height in the <code>viewDidLoad</code> of my ViewController so the method <code>func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat</code> returns instantly the right value. All of this works.</p>
  
  <p>In my UITableViewCell, I'm using many dynamic height labels (lines = 0). The layout is like this :</p>
  
  <p><a href="https://i.stack.imgur.com/Hp9Kk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hp9Kk.png" alt="enter image description here"></a></p>
  
  <p>I don't use transparent background, all my subviews are opaque with a specified background color. I checked with the <code>Color Blended Layers</code> (everything is green) and <code>Color Misaligned Images</code> (nothing is yellow).</p>
  
  <p>Here is my cellForRowAtIndexPath method :</p>
  
  <pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let tableViewSection = tableViewSectionAtIndex(indexPath.section)
      let tableViewRow = tableViewSection.rows.objectAtIndex(indexPath.row) as! TableViewRow
  
      switch tableViewRow.type! {
  
      case TableViewRowType.Article :
          let article = tableViewRow.article!
  
          if article.type == TypeArticle.Article {
  
              let cell = tableView.dequeueReusableCellWithIdentifier("articleCell", forIndexPath: indexPath) as! ArticleTableViewCell
              return cell
  
          } else {
  
              let cell = tableView.dequeueReusableCellWithIdentifier("chroniqueCell", forIndexPath: indexPath) as! ChroniqueTableViewCell
              return cell
  
          }
  
      default:
          return UITableViewCell()
  
      }
  }
  </code></pre>
  
  <p>And then, in the willDisplayCell method :</p>
  
  <pre><code>func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
      let tableViewSection = tableViewSectionAtIndex(indexPath.section)
      let tableViewRow = tableViewSection.rows.objectAtIndex(indexPath.row) as! TableViewRow
      let article = tableViewRow.article!
  
      if cell.isKindOfClass(ArticleTableViewCell) {
          let cell = cell as! ArticleTableViewCell
          cell.delegate = self
          cell.article = article
  
          if let imageView = articleImageCache[article.id] {
              cell.articleImage.image = imageView
              cell.shareControl.image = imageView
          } else {
              loadArticleImage(article, articleCell: cell)
          }
      } else {
          let cell = cell as! ChroniqueTableViewCell
          cell.delegate = self
          cell.article = article
  
          if let chroniqueur = article.getChroniqueur() {
              if let imageView = chroniqueurImageCache[chroniqueur.id] {
                  cell.chroniqueurImage.image = imageView
              } else {
                  loadChroniqueurImage(article, articleCell: cell)
              }
          }
      }
  }
  </code></pre>
  
  <p>All my images are downloaded in a background thread, so there is no image loading when scrolling.</p>
  
  <p>The layout is modified in my <code>ArticleTableViewCell</code> when I set the "article" property : <code>cell.article = article</code> :</p>
  
  <pre><code>var article: Article? {
      didSet {
          updateUI()
      }
  }
  </code></pre>
  
  <p>And my updateUI function :</p>
  
  <pre><code>func updateUI() -&gt; Void {
  
      if let article = article {
          if let surtitre = article.surtitre {
              self.surtitre.text = surtitre.uppercaseString
              self.surtitre.setLineHeight(3)
          } else {
              self.surtitre.hidden = true
          }
  
          self.titre.text = article.titre
          self.titre.setLineHeight(3)
  
          if let amorce = article.amorce {
              self.amorce.text = amorce
              self.amorce.setLineHeight(3)
          } else {
              self.amorce.hidden = true
          }
  
          if let section = article.sectionSource {
              if section.couleurFoncee != "" {
                  self.bordureSection.backgroundColor = UIColor(hexString: section.couleurFoncee)
                  self.surtitre.textColor = UIColor(hexString: section.couleurFoncee)
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem is when setting the label text, this causes the lag. The <code>setLineHeight</code> method transforms the text of the label into an NSAttributedString to specify the line height, but even when removing this code and simply setting the text label, a small lag occurs when displaying a new cell.</p>
  
  <p>If I remove all the label setup code, the cells displays the default label text and the tableview scroll is perfectly smooth, and the heights are correct too. Whenever I set a label text, the lag occurs.</p>
  
  <p>I'm running the app on my iPhone 6s. On the 6s simulator, there's absolutely no lag at all, perfectly smooth.</p>
  
  <p>Any idead? Maybe it's because I'm using a UIStackView to embed my labels? I did it so because it's easier to hide labels when empty, so the other elements are moved up, to avoid having spacing where the empty label is.</p>
  
  <p>I tried many thing but can't get the tableview to scroll smoothly, any help would be appreciated.</p>