title: iOS Swift - Filtering contents of UITableView that has custom cell with Search Bar and Search Display
body: <p>I have an issue with Search Bar and Search Display in a Swift project for iPhone. I added one UITableView with custom cells to a View. Cell have Indetifier set to "QuestionCell" and Class set to "QuestionCellTableViewCell". I added to that cell two label and I added outlets for the labels to cell class. 
  <strong>My goal is to filter the contents of the UITableView.</strong></p>
  
  <p>This is my ViewController:
  <strong>DomandeViewController.swift</strong></p>
  
  <pre><code>import UIKit
  
  class DomandeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate {
  
      var arrayDomande = [Domanda]()
      var areaDomande: Area = Area()
      var argomentoDomande: Argomento = Argomento()
      var domandeFiltrate = [Domanda]()
  
      @IBOutlet weak var questionTableView: UITableView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          arrayDomande = ModelManager.instance.getQuestionsWithArguments(inAreaId: areaDomande.id, aboutId: argomentoDomande.id)
          self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")
      }
  
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          if(tableView == self.searchDisplayController!.searchResultsTableView){
              return 1
          }else{
              return 1
          }
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          if tableView == self.searchDisplayController!.searchResultsTableView {
              return domandeFiltrate.count
          } else {
              return arrayDomande.count
          }
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
  
          var domanda = Domanda()
  
          if tableView == searchDisplayController!.searchResultsTableView {
              domanda = domandeFiltrate[indexPath.row]
          } else {
              domanda = arrayDomande[indexPath.row]
          }
  
          cell.testoLabel.text = domanda.testo
          cell.numeroLabel.text = String(domanda.numero)
  
          return cell
      }
  
      func filterContentForSearchText(searchText: String) {
          // Filter the array using the filter method
          filteredDomande = arrayDomande.filter({( domanda: Domanda) -&gt; Bool in
              let stringMatch = domanda.testo.rangeOfString(searchText)
              return stringMatch != nil
          })
      }
  
      func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -&gt; Bool {
          self.filterContentForSearchText(searchString)
          return true
      }
  }
  </code></pre>
  
  <p>This is my <strong>QuestionCellTableViewCell.swift</strong></p>
  
  <pre><code>import UIKit
  
  class QuestionCellTableViewCell: UITableViewCell {
  
      @IBOutlet weak var testoLabel: UILabel!
      @IBOutlet weak var numeroLabel: UILabel!
  
  
      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>
  
  <p>And this is my <strong>Domanda.swift</strong></p>
  
  <pre><code>import UIKit
  
  class Domanda: NSObject {
      var numero: Int = Int()
      var testo: String = String()
      var preferita: Bool = Bool()
      var visualizzato: Int = Int()
      var area: Int = Int()
      var argomento: Int = Int()
  }
  </code></pre>
  
  <p>Everything works fine, until I search a string in Search Bar: the application crashes and the console returns:</p>
  
  <blockquote>
    <p>"fatal error: unexpectedly found nil while unwrapping an Optional
    value"</p>
  </blockquote>
  
  <p>With debug I see that "cell" exists, but its outlets (testoLabel and numeroLabel) are nil and so the app crashes on</p>
  
  <pre><code>cell.testoLabel.text = domanda.testo
  </code></pre>
  
  <p>If I remove in viewDidLoad()</p>
  
  <pre><code>self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")
  </code></pre>
  
  <p>the app crashes when is entered some text in Search Bar with same error when executes </p>
  
  <pre><code>var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
  </code></pre>
  
  <p>If I add in viewDidLoad()</p>
  
  <pre><code>self.questionTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")
  </code></pre>
  
  <p>the app keep crashing at cell.testoLabel.text but this time at startup.</p>
  
  <p>Some advice for me?
  Thank you very much, I appreciate any help!</p>
  
  <p>Andrea</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Today I did other tests and here's the result: if I use the cells of searchResultsTableView as Basic cells everything works fine, but if I try to use the outlets of my custom cell the application crashes. This is the working code:</p>
  
  <pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
          var domanda = Domanda()
  
          if tableView == self.searchDisplayController!.searchResultsTableView {
              domanda = filteredDomande[indexPath.row]
  
              cell.textLabel.text = domanda.testo
          } else {
              domanda = arrayDomande[indexPath.row]
  
              cell.testoLabel.text = domanda.testo
              cell.numeroLabel.text = String(domanda.numero)
          }
  
          return cell
      }
  </code></pre>
  
  <p>In the end I can't seem to use my custom cell with searchResultsTableView. Where is my error? Thank you so much again!</p>