title: Swift 3.1 Label in TableViewCell - unexpectedly found nil
body: <p>I'm trying to create two tableViews in one UIViewController. But when I'm trying to assign value to UILabel, getting an error: fatal error: unexpectedly found nil while unwrapping an Optional value</p>
  
  <p>I wonder why, I have almost the same code for TableViewController with one tableView and it works with no issues. It looks like these UI Labels are not initialized when trying to assign value to it. But don't understand how to fix it.</p>
  
  <p>It fails here:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      var cell: UITableViewCell?
      if tableView == self.guestsTableView {
          let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell
          if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) {
              print(guestsTable.guestName) // works fine, prints the value
              print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value
              cell.guestNameLabel.text = guestsTable.guestName
              cell.openTimeLabel.text = String(describing: guestsTable.openTime)
              cell.cellDelegate = self
          }
      }
      else if tableView == self.ordersTableView {
          cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath)
          //to be done
      }
      // Configure the cell...
      return cell!
  }
  </code></pre>
  
  <p>Full code of this class:</p>
  
  <pre><code>import UIKit
  import CoreData
  
  class TableUIViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellWithButtonDelegate {
      //The following two variables will not be nil because prepare for segue will set them
      var tableName: String?
      var table: TablesTable? = nil
  
      fileprivate var currentTableSession: TableSessionTable? {
          get {
              let tableSessionTable = TableSessionTable()
              return tableSessionTable.getCurrentTableSession(table: table!)
          }
      }
      fileprivate var guestsTableFetchedResultsController: NSFetchedResultsController&lt;GuestsTable&gt;?
      fileprivate var ordersTableFetchedResultsController: NSFetchedResultsController&lt;OrdersTable&gt;?
  
      @IBOutlet weak var tableNameLabel: UILabel!
      @IBOutlet weak var tableCapacityLabel: UILabel!
      @IBOutlet weak var tableCountOfGuestsLabel: UILabel!
      @IBOutlet weak var tableDescriptionTextView: UITextView!
      @IBAction func closeTableButtonPressed(_ sender: UIButton) {
      }
      @IBOutlet weak var guestsTableView: UITableView!
      @IBOutlet weak var ordersTableView: UITableView!
  
      @IBAction func addGuestButtonPressed(_ sender: UIButton) {
          let guestsTable = GuestsTable()
          let tablesTable = TablesTable()
          let table = Table(tableName: tableName!, tableCapacity: 0, locationX: nil, locationY: nil, tableImage: nil)
          try? guestsTable.addNewGuest(table: tablesTable.getOrCreateTable(table: table))
          updateUI()
      }
      @IBAction func addOrderButtonPressed(_ sender: UIButton) {
      }
  
  
  
  
      override func viewDidLoad() {
          guestsTableView.dataSource = self
          guestsTableView.delegate = self
          guestsTableView.register(GuestAtTableTableViewCell.self, forCellReuseIdentifier: "guestCell")
  
          ordersTableView.dataSource = self
          ordersTableView.delegate = self
          ordersTableView.register(UITableViewCell.self, forCellReuseIdentifier: "orderCell")
          updateUI()
      }
  
      func didPressButton(table: TablesTable) {
      }
  
  
      private func updateUI () {
          let tableView = guestsTableView
          let context = AppDelegate.viewContext
          let request : NSFetchRequest&lt;GuestsTable&gt; = GuestsTable.fetchRequest()
          request.predicate = NSPredicate(format: "table= %@", currentTableSession!)
          request.sortDescriptors = [NSSortDescriptor(key: "guestName", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
          guestsTableFetchedResultsController = NSFetchedResultsController&lt;GuestsTable&gt;(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
          try? guestsTableFetchedResultsController?.performFetch()
          tableView?.reloadData()
      }
  
      private func updateUI1 () {
          let tableView = ordersTableView
          let context = AppDelegate.viewContext
          let request : NSFetchRequest&lt;OrdersTable&gt; = OrdersTable.fetchRequest()
          request.sortDescriptors = [NSSortDescriptor(key: "menuItem", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
          ordersTableFetchedResultsController = NSFetchedResultsController&lt;OrdersTable&gt;(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
          try? ordersTableFetchedResultsController?.performFetch()
          tableView?.reloadData()
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          var cell: UITableViewCell?
          if tableView == self.guestsTableView {
              let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell
              if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) {
                  print(guestsTable.guestName) // works fine, prints the value
                  print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value
                  cell.guestNameLabel.text = guestsTable.guestName
                  cell.openTimeLabel.text = String(describing: guestsTable.openTime)
                  cell.cellDelegate = self
              }
          }
          else if tableView == self.ordersTableView {
              cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath)
              //to be done
          }
          // Configure the cell...
          return cell!
      }
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          if tableView == self.guestsTableView {
              return guestsTableFetchedResultsController?.sections?.count ?? 1
          }
          else if tableView == self.ordersTableView {
              return ordersTableFetchedResultsController?.sections?.count ?? 1
          }
          else {return 1}
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          if tableView == self.guestsTableView {
              if let sections = guestsTableFetchedResultsController?.sections, sections.count &gt; 0 {
                  return sections[section].numberOfObjects
              }
              else {
                  return 0
              }
          }
          else if tableView == self.ordersTableView {
              if let sections = ordersTableFetchedResultsController?.sections, sections.count &gt; 0 {
                  return sections[section].numberOfObjects
              }
              else {
                  return 0
              }
          }
          else {return 0}
      }
  
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
          if tableView == self.guestsTableView {
              if let sections = guestsTableFetchedResultsController?.sections, sections.count &gt; 0 {
                  return sections[section].name
              }
              else {
                  return nil
              }
          }
          else if tableView == self.ordersTableView {
              if let sections = ordersTableFetchedResultsController?.sections, sections.count &gt; 0 {
                  return sections[section].name
              }
              else {
                  return nil
              }
          }
          else {return nil}
  
      }
  
      func sectionIndexTitles(for tableView: UITableView) -&gt; [String]? {
          if tableView == guestsTableView {
              return guestsTableFetchedResultsController?.sectionIndexTitles
          }
          else {
              return ordersTableFetchedResultsController?.sectionIndexTitles
          }
      }
  
      func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -&gt; Int {
          if tableView == guestsTableView {
              return guestsTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
          }
          else if tableView == ordersTableView {
              return ordersTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
          }
          else {return 0}
      }
  }
  </code></pre>
  
  <p>And full code of UITableViewCell class:</p>
  
  <pre><code>import UIKit
  
  class GuestAtTableTableViewCell: UITableViewCell {
      weak var cellDelegate: CellWithButtonDelegate?
      @IBOutlet weak var guestNameLabel: UILabel!
      @IBOutlet weak var openTimeLabel: UILabel!
      @IBAction func didPressButton(_ sender: UIButton) {
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
          // Configure the view for the selected state
      }
  }
  </code></pre>