Let my UISearchBar get focus automatically when the view is loaded - ios

Here is my view with UISearchBar in my navigationbar:
I want it get focussed automatically when my view is loaded. I tried a few ways based on this question. But none is working. I have to click the searchbar to make it focussed.
This is one of my code:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.searchController.isActive = true
DispatchQueue.main.async { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
}
}
Somebody mentioned the searchController should be active after becomeFirstResponder. I tried this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
self.searchController.isActive = true
}
}
The keybord did come out this time. But I can't key in anything in my search bar.
Any idea? Thanks.

I follow the suggestion and recreate my project. It works then. My code:
class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
......
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchController.isActive = true
}
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
}
}
Has to put becomeFirstRespnder in main queue, otherwise the keyboard will not be shown automatically.

I could focus the search bar whenever it is appear with the following code. Hope if would be helped.
And I have compared calling the become first responder inside viewWillAppear and viewDidAppear, it only worked when you call it inside viewWillAppear. But I am not quite understand why this happened. That maybe the reason why you can't type anything inside your searchbar.
PS: I think your DispatchQueue.main.async is not necessary inside viewDidAppear. It always be called in the main queue.
//
// TestSearchbarFocus.swift
// SwiftPlayground
//
// Created by Enix Yu on 31/10/2016.
// Copyright © 2016 RobotBros. All rights reserved.
//
import UIKit
class TestSearchbarFocus: UITableViewController, UISearchResultsUpdating {
var searchController : UISearchController!
let data = ["ABC", "BBC", "CCD", "Enix", "Peter", "Earth"]
var displayData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
navigationItem.titleView = searchController.searchBar
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
searchController.active = true
searchController.searchBar.becomeFirstResponder()
}
func filterDataForSearchText(text: String){
displayData = data.filter({
(item) -> Bool in
item.lowercaseString.containsString(text.lowercaseString)
})
tableView.reloadData()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterDataForSearchText(searchController.searchBar.text!)
}
// MARK : UITableViewDataSource/Delegate
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return displayData.count
}
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if searchController.active && searchController.searchBar.text != "" {
cell.textLabel?.text = displayData[indexPath.row]
} else {
cell.textLabel?.text = data[indexPath.row]
}
return cell
}
}
PS: I am using swift 2 + Xcode 7.3.1

Related

Swift search bar(controller) memory leaks

I have main screen, with a button on which I segue to searchVC screen.I have a navigation controller between them, in searchVC there are searchController and searchBar.
Problem:I need to activate search when screen appears, but searchBar activation(tap or becomeFirstResponder() ) causes memory leaks(image below)
I tried to remove delegates and the problem disappears, but I need to know when cancel button pressed to segue/dismiss to mainVC
Code:tableView for results, resultView with label for empty results
class SearchViewController: UIViewController,UISearchBarDelegate,UISearchControllerDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var resultView: ResultView!
let searchController = UISearchController(searchResultsController: nil)
var filteredSongs = [SongListModel]()
var songs = SongListModel.fetchSongs()
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search songs"
if #available(iOS 11.0, *) {
navigationItem.titleView = searchController.searchBar
navigationItem.hidesSearchBarWhenScrolling = false
// navigationController?.navigationBar.topItem?.searchController = searchController
// navigationItem.titleView?.isHidden = true
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
} else {
tableView.tableHeaderView = searchController.searchBar
}
searchController.searchBar.showsCancelButton = true
searchController.definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.delegate = self
searchController.searchBar.delegate = self
tableView.keyboardDismissMode = .interactive
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
delay(0.1) { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
}
}
func delay(_ delay: Double, closure: #escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
searchController.searchBar.resignFirstResponder()
}
func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
}
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredSongs = songs.filter({( song : SongListModel) -> Bool in
return song.musicFileName.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchController.searchBar.endEditing(true)
searchController.searchBar.resignFirstResponder()
// searchController.searchBar.delegate = nil
// searchController.searchResultsUpdater = nil
dismiss(animated: true, completion: nil)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PlayTilesSegue", let destinationVC = segue.destination as? TilesViewController, let selectedIndex = tableView.indexPathForSelectedRow?.row {
let song: SongListModel
if isFiltering() {
song = filteredSongs[selectedIndex]
} else {
song = songs[selectedIndex]
}
destinationVC.songFileName = song.musicFileName
navigationController?.setNavigationBarHidden(true, animated: false)
}
}
}
extension SearchViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
resultView.setIsFilteringToShow(filteredItemCount: filteredSongs.count, of: songs.count)
return filteredSongs.count
}
resultView.setNotFiltering()
return songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "filteredCell", for: indexPath) as! FilteredSongCell
let song: SongListModel
if isFiltering() {
song = filteredSongs[indexPath.row]
} else {
song = songs[indexPath.row]
}
cell.listenSongButton.setBackgroundImage(UIImage(named: "playback"), for: .normal)
cell.filteredAuthorNameLabel.text = song.authorName
cell.filteredSongNameLabel.text = song.songName
cell.playGameButton.setTitle(song.playButton.rawValue, for: .normal)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "PlayTilesSegue", sender: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
}
}
extension SearchViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}
Image memory leaks
Deactivating the search controller on removing VC from parent helps to avoid memory leak:
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil, searchController.isActive {
searchController.isActive = false
}
}
You need to set the UISearchController searchBar's delegate. Once you have done this, the addition of the delegate method searchBarCancelButtonClicked: will properly be called.
Here it is.

Screen lags when UISearchController dismissed

Expected:
When a UIButton is tapped, show a viewcontroller modally that has a search controller and tableview with results.
When tapping on an item in the list, change the text of the search bar to what was tapped and dismiss the viewcontroller back to the original with the UIButton now set to that text.
Actual:
UIButton calls a segue to the searchViewController.
searchViewController shows, and configures the searchController and tableView correctly.
Tapping on a cell calls the exit segue that unwinds to the original screen and correctly updates the text in the UIButton and searchbar...
but, a freaking white screen lags on the unwind segue and its driving me crazy.
Mitigation tried:
Resigning the searchController then calling the segue
programmatically
Calling self.dismiss(animated: true completion:nil) in didSelectRowAt
Putting the dismiss on the main thread with: DispatchQueue.main.async { }
calling self.presentingViewController?.dismiss(animated: true)
Video Demo of flashing
Code:
SearchDetailsViewController - the Viewcontroller to unwind to
import UIKit
class SearchDetailsViewController: UIViewController {
#IBOutlet weak var destinationButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if searchDestination != "" {
destinationButton.setTitle(searchDestination, for: UIControlState.normal)
destinationButton.setTitleColor(UIColor.black, for: UIControlState.normal)
} else {
destinationButton.setTitle("Search Nearby", for: UIControlState.normal)
}
}
#IBAction func unwindToSearchDetailsViewController(segue: UIStoryboardSegue){
}
}
SearchViewController - the problem child. I currently have the tableview cell as the exit segue in storyboard.
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
#IBOutlet weak var searchResultsTableView: UITableView!
var destinationsObj:[String:[String]] = [:]
var destinations:[String] = []
var defaultDestinations:[String] = ["Search Nearby"]
var filteredDestinations:[String] = ["Search Nearby"]
var shouldShowSearchResults = false
var searchActive:Bool = false
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
defaultDestinations = recentSearches
configureTableView()
configureSearchController()
}
override func viewDidAppear(_ animated: Bool) {
// Show search bar keyboard
searchController.isActive = true
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Configure Functions
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil) //nil lets the view controller also be the search results
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Where to?"
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
searchResultsTableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
}
func configureTableView() {
searchResultsTableView.delegate = self
searchResultsTableView.dataSource = self
//searchResultsTableView.isMultipleTouchEnabled = false
}
// MARK: TableView Delegate Functions
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filteredDestinations.count
} else {
return defaultDestinations.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idCell", for: indexPath)
if shouldShowSearchResults {
cell.textLabel?.text = filteredDestinations[indexPath.row]
} else {
cell.textLabel?.text = defaultDestinations[indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let value = tableView.cellForRow(at: indexPath)?.textLabel?.text {
self.searchController.searchBar.text = value
searchDestination = value
if !recentSearches.contains(value) {
recentSearches.append(value)
}
}
//self.searchController.resignFirstResponder()
// tableView.deselectRow(at: indexPath, animated: false)
// DispatchQueue.main.async {
// self.dismiss(animated: true, completion: nil)
// }
// self.performSegue(withIdentifier: "cancelSearchSegue", sender: self)
}
// MARK: UISearchBar Delegate Functions
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
//self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "cancelSearchSegue", sender: self)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let value = searchBar.text {
searchDestination = value
if !recentSearches.contains(value) {
recentSearches.append(value)
}
}
//self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "cancelSearchSegue", sender: self)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
shouldShowSearchResults = true
if searchText.characters.count > 1 {
return
} else {
if let firstLetter = searchText.characters.first{
print("Typed \(firstLetter)")
getPredictionData(firstLetter:firstLetter.description)
}
}
}
func dismissCurrentView() {
// self.presentingViewController?.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "cancelSearchSegue", sender: self)
}
Screenshot of my storyboard
Well I thought I post the answer incase this happens to anyone else.
In ViewDidAppear, I was setting the searchController to active
searchController.isActive = true
Well before I dismissed, I needed to set it to inactive!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let value = tableView.cellForRow(at: indexPath)?.textLabel?.text {
self.searchController.searchBar.text = value
searchDestination = value
if !recentSearches.contains(value) {
recentSearches.append(value)
}
}
self.searchController.isActive = false
self.performSegue(withIdentifier: "cancelSearchSegue", sender: self)
}
Don't perform segue, try to dismiss view controller directly and before dismissing set the property of presenting view controller to the search result

Cannot Select TableViewCell when Search Bar is active - iOS Swift

I have a search bar that is connected to a table view. It works exactly as I want when the search bar is not active but cell selection is disabled when the search bar is active. I've debugged it and didSelectRowAtIndexPath is not even being called when I select a row when search is active. What could be causing this?
Here's the relevant code:
class FabricsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = false
searchController.hidesNavigationBarDuringPresentation = false
myTableView.tableHeaderView = searchController.searchBar
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if searching {
searching = false
searchBar?.resignFirstResponder()
FirebaseClient.sharedInstance.joinFabric(uid: self.appDelegate.uid!, fabricKey: allFabrics[indexPath.row].key)
updateFabricList()
} else {
appDelegate.selectedFabricKey = joinedFabrics[indexPath.row].key
performSegue(withIdentifier: "fabricSelected", sender: self)
}
myTableView.deselectRow(at: indexPath, animated: false)
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
if let allFabrics = allFabrics {
filteredFabrics = allFabrics.filter { fabric in
return (fabric.name.lowercased().contains(searchText.lowercased()))
}
myTableView.reloadData()
myTableView.setContentOffset(CGPoint.zero, animated: false)
}
}
}
extension FabricsViewController: UISearchResultsUpdating {
func updateSearchResults(for: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
Please check the text for your method name.
Make sure that you are using method name as below:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
You have set the delegate of searchBar to self. Are you activating any tap gestures when search becomes active? This tap gesture can interrupt your didSelectRowAtIndexPath method.
For those who are facing same issue, according Apple's documentation you should have following code for add searchController:
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.yourTableView.tableHeaderView = searchController.searchBar
}

UISearchController causes black screen Swift 2.0

I have a strange problem in iOS 9 with Swift 2.0. I added UISearchController in my tableViewController but it causes a strange black screen problem. When I press the search bar and write something it shows my filtered results without any problem but when I tap another tab bar button like Bookmarks and after that when I tap tableViewController which is Most Viewed again it shows black screen like screen shot.
There is my tableViewController;
import UIKit
class CitiesTableViewController: UITableViewController, UISearchResultsUpdating {
// MARK: - Class Properties
private var cities = [String]()
private var veterinaries = [Veterinary]()
private var filteredVeterinaries = [Veterinary]()
private var resultSearchController: UISearchController!
// MARK: - TableViewController Life Cycle Methods
override func viewDidLoad() {
super.viewDidLoad()
self.getCitiesList()
self.configureResultsSearchController()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
self.resultSearchController.active = false
}
// MARK: - Configuring Search Bar Controller
private func configureResultsSearchController() {
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.hidesNavigationBarDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.resultSearchController.searchBar.placeholder = "Klinik veya ilçe adı"
self.tableView.tableHeaderView = self.resultSearchController.searchBar
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.resultSearchController.active { return self.filteredVeterinaries.count }
else { return self.cities.count }
}
// MARK: - Table view Delegate Methods
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (self.resultSearchController.active) {
self.performSegueWithIdentifier(Constants.ShowDetailViewControllerSegueIdentifier, sender: nil)
} else {
self.performSegueWithIdentifier(Constants.ShowTownsTableViewControllerSegueIdentifier, sender: nil)
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellIdentifier, forIndexPath: indexPath)
if (self.resultSearchController.active) {
cell.textLabel?.text = self.filteredVeterinaries[indexPath.row].name
cell.detailTextLabel?.text = self.filteredVeterinaries[indexPath.row].address
return cell
} else {
cell.textLabel?.text = self.cities[indexPath.row]
return cell
}
}
// MARK: - PARSE Query Methods
private func getCitiesList() {
let parseQueries = ParseQueries()
parseQueries.downloadListData() {
(let parseResults) in
if let veterinaries = parseResults as? [Veterinary] {
self.veterinaries = veterinaries
for vet in veterinaries {
if let city = vet.city {
self.cities.append(city)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.cities = HelperMethods().removeDuplicatesAndSort(array: self.cities)
self.tableView.reloadData()
}
}
}
}
// MARK: - UISearchController Delegate Methods
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredVeterinaries.removeAll(keepCapacity: false)
if let searchBarText = searchController.searchBar.text{
let searchText = searchBarText.lowercaseString
// Searching with Veterinary Name and Veterinary City
self.filteredVeterinaries = self.veterinaries.filter({$0.name?.lowercaseString.rangeOfString(searchText) != nil})
self.filteredVeterinaries += self.veterinaries.filter({$0.town?.lowercaseString.rangeOfString(searchText) != nil})
tableView.reloadData()
}
}
This is the black screen image from iOS 9 simulator same as real device.
I think its deiniting my tableView when I tap the searchBar and it can't init again. Is this a bug or something ?
How can I solve this problem ?
Thank you !
Friend, in your viewDidLoad() insert this code line:
self.definesPresentationContext = true
See how I put (line 29):
click here to see
I recently faced the same issue and I could fix it easily.
self.definesPresentationContext = true was already defined in viewDidLoad() but it didn't solve this issue.
In the above question, maybe you show CitiesTableViewController directly when you tap MostViewed tabbar item.
In order to avoid the black screen, you can embed CitiesTableViewController in a UINavigationController and try to show the navigationcontroller when tapping the tabbar item.
This solution will avoid the black screen issue.

Why is my tableview blank after using UISearchResultsUpdating cancel button?

I have a tableview that loads a function and displays data in viewDidAppear. The same tableview (I assume) is used when a user taps on the searchBar in the navigation title, and searches for a query.
The problem is after the user taps the cancel button: the function for calling the original data performs and prints the correct data, but doesn't appear on the screen after tableView.reloadData.
I've tried placing the function in various places (didCancel, didEndEditing), and the function is called/correctly returns data, but doesn't appear on the table.
Any suggestions or workarounds?
class LocationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(animated: Bool) {
self.load0()
tableView.reloadData()
self.locationSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.delegate = self
controller.searchBar.searchBarStyle = .Minimal
controller.searchBar.sizeToFit()
self.navigationItem.titleView = controller.searchBar
return controller
})()
}
//below I try to remove the search data and reload the original data.
override func viewDidDisappear(animated: Bool) {
self.locationSearchController.active = false
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData()
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData() }
The function that performs the search is updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) { query and places items in an array that's displayed by the original tableView }
I wondered if there was a tableView being used that I was not aware of, but it's clear that the searchController is using the original tableView because it follows the design for my reusable cell.
You must fill your UITableView this way:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.searchDisplayController!.active {
return self.filteredData.count
}
return self.defaultdData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
if self.searchDisplayController!.active {
cell.textLabel.text = self.filteredData[indexPath.row]
} else {
cell.textLabel.text = self.defaultData[indexPath.row]
}
return cell
}

Resources