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
}
Related
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
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
}
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
While searching a tableView, every time I try to select a row it just takes me back to the unsearched tableView. What am I missing? the segue works perfectly when not filtering through the table. The ability to select a row just disapears while the searchBar is activated.
import UIKit
import Foundation
class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating {
var WodList = [WOD]()
var FilteredWodList = [WOD]()
var Keyword = ""
var searchController : UISearchController?
var index = Int()
#IBAction func backButton(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
for wodData in BenchmarkWODs.library {
let wod = WOD(dictionary: wodData)
WodList.append(wod)
}
// Search Bar
self.searchController = UISearchController(searchResultsController: nil)
self.searchController?.searchBar.autocapitalizationType = .None
self.tableView.tableHeaderView = self.searchController?.searchBar
self.searchController?.searchResultsUpdater = self
self.Keyword = ""
definesPresentationContext = true
self.filterByName()
}
func filterByName(){
self.FilteredWodList = self.WodList.filter({ (wod: WOD) -> Bool in
if self.Keyword.characters.count == 0 {
return true
}
if (wod.name?.lowercaseString.rangeOfString(self.Keyword.lowercaseString)) != nil {
return true
}
return false
})
self.tableView.reloadData()
}
// Search Bar Function
func updateSearchResultsForSearchController(searchController: UISearchController) {
Keyword = searchController.searchBar.text!
self.filterByName()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.FilteredWodList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell
let wod = self.FilteredWodList[indexPath.row]
if let wodName = wod.name {
cell.textLabel?.text = wodName
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.filterByName()
self.performSegueWithIdentifier("showBenchmarkDetail", sender: nil)
}
}
Figured it out after playing around. Apparently adding the below code corrects the problem.
searchController?.dimsBackgroundDuringPresentation = false
swift 'dimsBackgroundDuringPresentation' was deprecated in iOS 12.0 Use the obscuresBackgroundDuringPresentation property instead.
searchController?.obscuresBackgroundDuringPresentation = false
searchController.obscureBAckgroundDuringPresentation = false is deprecated in IOS 12.0, so for me it was issue with other gesture detector added to the tableview , so make sure you dont have any other gesture detector and touchesview method that distort the normal working flow of tablview's delegate method( didSelectAtRow ), hope it will work,
theres any way to hide the cancel button on UISearchController?
The other behaviour i would like to know if its possible, is when the user press the Cancel Button, to set the text on the UISearchBar.
Thanks
Using seachController.searchBar.setShowsCancelButton(false, animated:false) still seems to not do it for this.
Try adding the UISearchBarDelegate to your controller and assign the searchBar's delegate to self. Then you can use the searchBarShouldBeginEditing to edit its visibility.
import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate {
var searchController: UISearchController!
let menuItems: [String] = ["Row 1", "Row 2", "Row 3"]
override func viewDidLoad() {
super.viewDidLoad()
let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultsViewControllerStoryboardIdentifier") as! SearchResultsTableViewController
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
}
#IBAction func searchButtonPressed(sender: UIBarButtonItem) {
presentViewController(searchController, animated: true, completion: nil)
}
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
searchController.searchBar.setShowsCancelButton(false, animated: true)
return true
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = self.menuItems[indexPath.row]
return cell
}
}