Q:

Swift/iOS 8: search bar raising fatal error: unexpectedly found nil while unwrapping an Optional value

Following this tutorial, I have just added a "Search Bar and Search Display Controller" to my Table View Controller. 
As you can see from the following screenshot, the table and the search bar are correctly loaded:

by using "Cell" as cell reuse identifier.
Anyway, there are two problems:
1) When the search bar is tapped it simply disappears under the navigation bar even if it still accepts text to search

2) as soon as I start typing something in the search bar (even if it is "hidden") then an exception mentioned in title is raised.
Here is my code where the line with /***/ is where the exception raises:
import UIKit

class AllTasksViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {

  var allTasks = [Task]()
  var taskService = TaskService()
  var organizedTasks = TaskMenuItems()
  var filteredTasks = [Task]()

  override func viewWillAppear(animated: Bool) {
      self.tabBarController?.navigationItem.setHidesBackButton(true, animated: true)
      self.tabBarController?.navigationItem.rightBarButtonItem =  self.editButtonItem()
      if (LoggedUser.isLogged) {
          self.navigationItem.setHidesBackButton(false, animated: true)
          self.taskService.requestAllTasks {
              (response) in
              self.allTasks = self.taskService.loadTasks(response) as! [Task]
              self.organizedTasks.organize(self.allTasks)
              /*println(self.organizedTasks.items["Aperti"]?.count)
              println(self.organizedTasks.items["Chiusi"]?.count)
              println(self.organizedTasks.items["Scaduti"]?.count)
              println(self.organizedTasks.items["Sospesi"]?.count)*/
              dispatch_async(dispatch_get_main_queue()) {
                  self.tableView.reloadData()
              }
          }
      }
  }

  override func viewDidLoad() {
      super.viewDidLoad()
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }

  /* Number of sections */
  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return self.organizedTasks.sections.count
  }

  /* Number of rows for each section */
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      if tableView == self.searchDisplayController!.searchResultsTableView {
          return self.filteredTasks.count
      }

      switch section {
          case 0:
              return self.organizedTasks.items["Aperti"]!.count
          case 1:
              return self.organizedTasks.items["Chiusi"]!.count
          case 2:
              return self.organizedTasks.items["Scaduti"]!.count
          case 3:
              return self.organizedTasks.items["Sospesi"]!.count
          default:
              return -1
          }

  }

  override func tableView(tableView: UITableView,
      cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

          l?
          var selectedStatus:String
          let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell /***/

          var task:Task

          if tableView == self.searchDisplayController!.searchResultsTableView {
              task = filteredTasks[indexPath.row]
              cell.textLabel?.text = task.titolo
              cell.detailTextLabel?.textColor =  Settings.decideColor(task.priorita)
              cell.detailTextLabel?.text = task.priorita
              return cell
          }

          switch indexPath.section {
          case 0:
              /*let cell =   tableView.dequeueReusableCellWithIdentifier("OpenTasks",
                forIndexPath: indexPath) as! UITableViewCell*/
              selectedStatus = "Aperti"
              break
          case 1:
            /*let cell =   tableView.dequeueReusableCellWithIdentifier("ClosedTasks",
                forIndexPath: indexPath) as! UITableViewCell*/
              selectedStatus = "Chiusi"
              break
          case 2:
              /*let cell = tableView.dequeueReusableCellWithIdentifier("ExpiredTasks",
                forIndexPath: indexPath) as! UITableViewCell*/
              selectedStatus = "Scaduti"
              break
          case 3:
              /*let cell = tableView.dequeueReusableCellWithIdentifier("SuspendedTasks",
                  forIndexPath: indexPath) as! UITableViewCell*/
            selectedStatus = "Sospesi"
              break
          default:
              /*let cell = tableView.dequeueReusableCellWithIdentifier("",
                forIndexPath: indexPath) as! UITableViewCell*/
              selectedStatus = ""
              break

          }

          task = self.organizedTasks.items[selectedStatus]![indexPath.row]
          cell.textLabel?.text = task.titolo
          cell.detailTextLabel?.textColor = Settings.decideColor(task.priorita)
          cell.detailTextLabel?.text = task.priorita
          return cell

  }

  override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
      switch (section) {
      case 0:
          let count = self.organizedTasks.items["Aperti"]!.count
          return "Aperti (\(count))"
      case 1:
          let count = self.organizedTasks.items["Chiusi"]!.count
          return "Chiusi (\(count))"
      case 2:
          let count = self.organizedTasks.items["Scaduti"]!.count
          return "Scaduti (\(count))"
      case 3:
          let count = self.organizedTasks.items["Sospesi"]!.count
          return "Sospesi (\(count))"
      default:
          return ""
      }
  }

  func filterContentForSearchText(searchText: String, scope:String="All") {
      // Filter the array using the filter method
      self.filteredTasks = self.allTasks.filter({( task: Task) -> Bool in
          let categoryMatch = (scope == "All") || (task.priorita == scope)
          let stringMatch = task.titolo.rangeOfString(searchText)
          return categoryMatch && (stringMatch != nil)
      })
  }

  func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
      self.filterContentForSearchText(searchString)
      return true
  }

  func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
      self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
      return true
  }

  // In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)     {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
  }

}

I don't know why these two things happen. It seems that it can't create another cell with "Cell" identifier but this is strange because when the table is loaded (without using the search bar) all is ok so the identifier Cell is registered.
Please, can you help?

A:

Try the following code where you have configured your search display controller or in viewDidLoad:
self.searchDisplayController.searchResultsTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
Also, use tableView.
dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
It's worth mentioning that, UISearchDisplayController is deprecated in iOS 8, use UISearchController instead.

