SearchResultsUpdating not updating - swift

Ok so I have a UITableViewController and created a SearchViewController to search through an array of investments I made. The investments display in the UITableView (by alphabetical order), but when I click search and type in some letters, there in no updating to show investments with those letters.
I feel like this is a simple mistake I made and it would really help if I could get some tips and help.
Here are a couple images of the code I have right now. (Sorry if I included some unnecessary code)
and also this one:
Thanks guys and hope that it can be solved.

What i would recommend you to do is to implement the UISearchBarDelegate methods an there you can do search by word or when user types the search on the keyboard where you should filter the dataSource and then reset the tableview data (this will update your table with the new data the user is searching for)
In your implementation you did not added
searchbar.delegate = self
so the search doesn't know you are searching
EDIT: to implement UISearchBarDelegate to as follows
class NewTableViewController: UITableViewController {
ovveride viewDidLoad() {
super.viewDidLoad()
searchbar.delegate = self
}
}
extension NewTableViewController: UISearchBarDelegate {
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
search()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
search()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
search()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
search()
}
func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) {
search()
}
}

Related

Xcode search bar not reacting to changing text or cancel being clicked

My code for search bar delegate is:
extension ViewController: UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// change the array that is being printed
searching = true
print("here")
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
print("cancel")
tableView.reloadData()
}
}
My search bar is connected as #IBOutlet weak var searchBar: UISearchBar!
Setting aside whether I'm filtering things right, right now neither typing into the search bar nor clicking cancel does anything (as shown above, at least when those methods get called "here" and "cancel" should be printed to the console), and cancel is not clearing the search bar text either. Does anyone know why this happens/what I did wrong?
Have you checked if you set the searchBar delegate as the ViewController? You've done right implementing the protocol, but make sure you have this code in your viewDidLoad function:
self.searchBar.delegate = self
Is also worth checking if the storyboard search bar element is really connected to the #IBOutlet declared on the ViewController. You can find this in the connections inspector. Yours must look like this:
Look at the second connection in this image

How i use UISearchController in UICollectionViewController using Swift

I want to implement UISearchController in UICollectionViewController. But i don't want to implement search in CollectionView I need SearchController in Swift.
I need to show Search Controller on Search Click
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { }
func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) { }
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.view.addSubview(searchController.view)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { }
func updateSearchResultsForSearchController(searchController: UISearchController) {}
func updateSearchResults(for searchController: UISearchController) { }
I am adding when search is active but not showing
let searchController = UISearchController(searchResultsController: nil)
Please suggest me if any one have any idea .

Use the same search method in the whole app in Swift

I'm new in Swift and develop an app with a glossary in one controller with a searchbar and methods associated through UISearchBarDelegate. And I'd like to use exactly the same methods in other controllers.
I already put the glossary data in a singleton, but how to use the search methods without copying all the methods. Can I simply make the other controller inherit from my GlossaryController? But what about the protocol?
You could create a new swift file and put your methods there. Then, they will be accessible from any other file you have...
#Sotiris:
Exactly. I override several methods from search delegate, like
// MARK: - Search Bar delegate
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { }
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { }
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { }
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { }
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {}
As my controllers implement UISearchBarDelegate, they need to override this methods (or some of them).
If I create a class that implement the delegate and override these methods, can I call this new class in controllers without they don't need to implement UISearchBarDelegate anymore.

How to make keyboard dismiss when I press out of searchbar on Swift?

I try to make my searchbar on swift, but I have a problem to dismiss keyboard on screen when I pressed out of searchbar. When I try with textfield, it works perfectly fine with this code.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
It work when i press out of my textfield and then the keyboard is gone. I want to make like that with my searchbar, because when I use searchbar and use the same way like textfield, it doesn't work at all. Any reference or code is very useful for me.
try this :
self.mySearchController.searchBar.endEditing(true)
replace mySearchController with your created controller name..
If you did not create it programmatically but instead you just dragged a search bar from library then IBoutlet your searchable to your class and reference it as:
self.mySearchBar.endEditing(true)
I found it easier and simplier to use Table View for dismissal. (If you're using table view)
Swift 4:
self.tableView.keyboardDismissMode = .onDrag
Tested and working!
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
searchActive = false;
self.mySearchBar.endEditing(true)
}
Edit for Swift 4.2
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchActive = false
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
searchProject.resignFirstResponder()
}
This method will be invoked when user click search button on keyboard.So here we can dismiss keyboard.I think this is the right method.
Firstly, Apple's UISearchBarDelegate is the correct solution to hide keyboard when users click a search button while UISearchBar's instance is the first responder (learn UIResponder). In short, searchBarSearchButtonClicked(_:) is what you need for this task.
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder() // hides the keyboard.
doThingsForSearching()
}
If it doesn't work, check, does your controller conform to UISearchBarDelegate and secondly, does UISearchBarDelegate know about your class implementation (if you don't quite understand what am I talking about, you should learn delegation pattern starting to read here):
class YourAwesomeViewController: UIViewController, UISearchBarDelegate { // pay attention here
#IBOutlet weak var yourSearchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.yourSearchBar.delegate = self // and it's important too
}
}
Further, if you need to hide the keyboard touching outside of search bar without touching the search button (the user may change his mind to search something), UITapGestureRecognizer is a simple way too to deal with that.
Ctrl-drag a Tap Gesture Recognizer from the Object Library to your View Controller.
Ctrl-drag the recently added Tap Gesture Recognizer from the document outline in the storyboard to your class implementation as IBAction.
Finally, write a code:
#IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) {
self.yourSearchBar.resignFirstResponder()
}
Also, don't forget to create #IBOutlet for the search bar to have an access inside your class implementation.
Both variants above work well in my project.
Swift 4+:
You can try, creating a tap gesture and add in the self.view
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(sender:)))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(singleTapGestureRecognizer)
and in selector func you call self.searchBar.resignFirstResponder
#objc func singleTap(sender: UITapGestureRecognizer) {
self.searchBar.resignFirstResponder()
}
You can use a general UIViewController extension
Just add a new swift file on the project and paste the following code snippet
Code
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard(_:)))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
if let nav = self.navigationController {
nav.view.endEditing(true)
}
}
}
Now call hideKeyboardWhenTappedAround() from viewDidLoad method where you want keyboard hiding feature.
class MaCaveViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
// When button "Search" pressed
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
print("end searching --> Close Keyboard")
self.searchBar.endEditing(true)
}
}
This works very well for me.
we can do this with following methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = false;
}
This works for me in Swift 4
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
self.searchBar.endEditing(true)
}

Swift how to update the Search result when touch return key

I Swift, i make a Search View using UISearcheController and UISearcheResultUpdating
and i update the result with func updateSearchResultsForSearchController
if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {
let searchBarText = self.searchController!.searchBar.text
var arrResult = DFManager.GetResult(searchBarText)
self.results?.addObjectsFromArray(arrResult)
// Reload a table with results.
self.searchResultsController?.tableView.reloadData()
}
But that is result always updating when i type char by char, it make this app slowly. I want this result only update when type return key?
Try this:
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
//do something
searchBar.resignFirstResponder() //hide keyboard
}
And dont forget to add the searchBar delegate:
class SomeViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
searchBar.delegate = self
}
}