I have a UITableViewController (with 6 cells) and a UISearchController. Whenever I tap on the search bar, the table view cells disappear and are replaced with an empty tableView until I start typing something and the search results appear. How can I make it so that cells stay underneath when I tap the search bar? Here is my code pertaining to the SearchController:
In viewDidLoad():
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.sizeToFit()
controller.searchBar.barTintColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
return controller
})()
The rest:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredTableData.count
} else {
return self.items.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
var item : TextCell
if (self.resultSearchController.active) {
item = filteredTableData[indexPath.row]
return cell
} else {
item = items[indexPath.row]
cell.userText.text = item.userText
cell.userPreferredName.text = item.userPreferredName
cell.userName.text = item.userName
cell.userImage.layer.cornerRadius = 23.75
cell.userImage.clipsToBounds = true
cell.userImage.layer.borderWidth = 0.3
cell.userImage.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.time.text = "\((indexPath.row + 1) * 3)m"
return cell
}
}
}
extension TableViewController: UITableViewDelegate {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
extension TableViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
var filteredArray : [TextCell] = [TextCell]()
for textCell in self.items
{
if (textCell.userName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userPreferredName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userText.rangeOfString(searchController.searchBar.text) != nil)
{
filteredArray.append(textCell)
}
}
filteredTableData = filteredArray
self.tableView.reloadData()
}
}
Screenshots:
Bonus: Is there any way of making those cells selectable?