title: iOS with Swift: App crashes on UITableView scroll down event
body: <p>I am writing an iOS app in Swift that puts data from an API into a TableView. The goal is that the user can scroll down a continuous list of blog posts. What is happening is that data is filling the first screen's amount of cells fine, but when the user scrolls down, the app crashes as one of the cell properties, <code>cellImageView</code> becomes <code>nil</code> and is unwrapped (as force unwrap is specified in the cell class's code):</p>
  
  <pre><code>class BlogPostEntryCell: UITableViewCell {
      //MARK: Properites
      @IBOutlet weak var titleLabel: UILabel!
      @IBOutlet weak var bodyTextView: UITextView!
      @IBOutlet weak var initialsImageView: UIImageView!   
  }
  </code></pre>
  
  <p>Here is the code that causes the crash:</p>
  
  <pre><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCell
  
      apiCaller.getAPIDataThroughAsyncCall(id: nextCell, entry: { cellContent in
          DispatchQueue.main.async(execute: {
              cell.cellLabel.text = cellContent.labelText
              cell.cellTextView.text = cellContent.textViewText
              // App crashes at the below line
              cell.initialsImageView = self.createPicture(initials: cellContent.pictureText)
          })
      })
  
      nextCell += 1
  
      return cell
  }
  </code></pre>
  
  <p>The interesting thing is that if do this:</p>
  
  <pre><code>    cell.cellLabel?.text = cellContent.labelText
      cell.cellTextView?.text = cellContent.textViewText
      // App does NOT crash at the below line
      cell.initialsImageView? = self.createPicture(initials: cellContent.pictureText)
  </code></pre>
  
  <p>, the app doesn't crash but the data just repeats over and over again as the user scrolls.</p>
  
  <p>I've tried the solution to a similar problem here: <a href="https://stackoverflow.com/questions/41123465/why-does-data-returned-from-coredata-become-nil-after-tableview-scroll-in-swift">Why does data returned from coredata become nil after tableview scroll in swift?</a>, but that did not work. However, I do believe this has to do with the asynchronous API calls.</p>
  
  <p>I am thinking that possible solutions may include population an array of API data before generating the cells, but I would like some insight on the source of this problem before a rethink the architecture of the code.</p>