im doing a simple project in Xcode 6 and i want to add search bar in tableviewcontroller but something is not working for me. Im doing it by this tutorial http://www.raywenderlich.com/76519/add-table-view-search-swift
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}
here im getting error "fatal error: unexpectedly found nil while unwrapping an Optional value". idk why. whole code is here
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
var program : Program
if tableView == self.searchDisplayController!.searchResultsTableView {
program = filteredProgramy[indexPath.row]
} else {
program = programy[indexPath.row]
}
func filterContentForSearchText(searchText: String) {
// Filter the array using the filter method
var scope = String()
self.filteredProgramy = self.programy.filter({( program: Program) -> Bool in
let categoryMatch = (scope == "All") || (program.category == scope)
let stringMatch = program.name.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
}
}