Search Bar controller in TableView - ios

Hello I am trying to implement a SearchBarController but If I type something in the search bar data doesn't comes up according to the letters typed. Here is my code
class CountriesTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filterTableData = NSDictionary()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
getCountriesNamesFromServer()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.resultSearchController.active){
return self.filterTableData.count-1
}else {
return dict.count-1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountriesTableViewCell
if(self.resultSearchController.active){
// (cell.contentView.viewWithTag(1) as! UILabel).text = filterTableData[indexPath.row]
cell.countryNameLabel.text = (((filterTableData["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}else{
cell.countryNameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let keys: NSArray = dict.allKeys
let filteredKeys: [String] = keys.filteredArrayUsingPredicate(NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)) as! [String]
filterTableData = dict.dictionaryWithValuesForKeys(filteredKeys)
}
I think Problem is in this function updateSearchResultsForSearchController because first I think I have to remove the object from Nsdictionary. I know in Array we do this
filterTableData.removeAll(keepCapacity: false)
and I think for NSDictionary I have to use NSMUtableDictionary But I don't know if that is the problem. If it is the problem still don't know how can I remove objects from NSMutableDictionary and in short to make this search functionality workable.
Note: Data is Populating successfully in tableView.. just the search functionality is not working

Related

searching and filter array firebase data swift3

My app crashing while search a text in a searchbar with error: thread1:signal SIGABRT probably the problem updateSearchResults() method?
or type of array? I'm beginner with swift any idea?
#IBOutlet weak var tableView: UITableView!
var data = [Any]()
var ref:FIRDatabaseReference!
// Filter Data from Firebase
var filteredData = [Any]()
// Declare searchBar
let searchController = UISearchController(searchResultsController: nil)
//is the device landscape or portrait
var isPortraid = true
#IBOutlet weak var bannerView: GADBannerView!
func fetchDataFromFirebase(){
EZLoadingActivity.show("caricamento...", disableUI: true)
ref = FIRDatabase.database().reference()
ref.observe(.value, with: { (snapshot) in
let dataDict = snapshot.value as! NSDictionary
self.data = dataDict["data"] as! [Any]
self.filteredData = self.data
print ("Sacco di merda:\(self.filteredData)")
self.tableView.reloadData()
EZLoadingActivity.hide()
})
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
fetchDataFromFirebase()
// Implement searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
//TableView Data Source and Delegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for:indexPath) as! MainScreenTableViewCell
let rowData = self.filteredData[indexPath.row] as! NSDictionary
let imageName = rowData["imageName"] as! String
cell.backgroundImageView.image = UIImage(named: imageName)
let label = rowData["categoryName"] as! String
cell.mealCategoryLabel.text = label
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let categoryViewController = storyboard.instantiateViewController(withIdentifier: "CategoryViewController") as! CategoryViewController
let rowData = self.data[indexPath.row] as! NSDictionary
categoryViewController.categoryTitle = rowData["categoryName"] as! String
let categoryData = rowData["category"] as! [Any]
categoryViewController.data = categoryData
self.navigationController?.pushViewController(categoryViewController, animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isPortraid {
return UIScreen.main.bounds.height/3
} else {
return UIScreen.main.bounds.height/1.2
}
}
//Method for update search
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == ""{
filteredData = data
} else {
filteredData = data.filter{($0 as AnyObject).contains(searchController.searchBar.text!)}
}
self.tableView.reloadData()
}
if searchController.searchBar.text! == ""
This is almost certainly the offender. The text property on UI objects is typically nil when it's empty, so when you force unwrap it your app crashes. You should never force unwrap something unless you are absolutely certain it will never be nil at that point.
There's a couple different ways you can handle this, which basically amount to making sure text isn't nil before you do anything with it.
Personally I would rewrite the if statement to unwrap the optional for the non-empty case:
if let text = searchController.searchBar.text, text != "" {
filteredData = data.filter{($0 as AnyObject).contains(text)}
} else {
filteredData = data
}
You could also use nil-coalescing:
if (searchController.searchBar.text ?? "") == ""
but personally I prefer to write it to avoid force unwrapping even when you're sure it isn't nil, so I would recommend the first one.

How could I add UISwitch to Uitableview

I could not find any way to add switch button in my tableview
I have a search bar which works perfectly,
and now I want to add a toggle button to enable "AND" "OR" filter in my search.
import Foundation
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
var appDel: AppDelegate!
// var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "my.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "my.png"))
var filteredTableData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
self.edgesForExtendedLayout = UIRectEdge.None
UIApplication.sharedApplication().statusBarHidden = true
appDel = UIApplication.sharedApplication().delegate as? AppDelegate
// tabBarItem = customTabBarItem
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
print("TableView called!!")
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
//refer to http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = ((appDel.dataFetcher?.appTitles)! as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else if appDel.dataFetcher?.appTitles == nil {
return 0
}else{
return (appDel.dataFetcher?.appTitles?.count)!
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("appCell", forIndexPath: indexPath)
let currentItem: String = (appDel.dataFetcher?.appTitles?[indexPath.row])!
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = currentItem
cell.detailTextLabel?.text = String(indexPath.row)
return cell
}
}
}
Create a table header view by dragging a UIView to the top of the tableView, just above the first prototype cell. See this question for an example.
There you can place your UISwitchView and create an outlet to your VC.

Using UISearchController to search a UITableView in a different View Controller

I'm trying to implement a UISearchController to reference and search for items in a UITableView from another View Controller. In other words, I'd like to have search bar in VC1 to search for items in the UITableView of VC2.
The reason I'm doing this is because I'd like to have search functionality right when my app opens, i.e. in VC1 instead of having to go VC1->V2 to search for items in VC2.
I have already created some simple code to search for items in the UITableView of VC1, but how can I reference the UITableView of VC2 instead?
Here's my sample code for UISearchController implementation:
let tableData = ["One","Two","Three","Twenty-One"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active && self.resultSearchController.searchBar.text != "") {
return self.filteredTableData.count
}
else {
return self.tableData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if (self.resultSearchController.active && self.resultSearchController.searchBar.text != "") {
filteredTableData = tableData
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
FYI: Both my VC1 and VC2 are connected via a UINavigationController
Thanks!

how to hide the navigation bar after dismissing from searchController

I have two viewControllers ControllerA and ControllerB. Both doesn't have NavigationBar as I have hided the navigationBar in both the controllers. On ControllerB there is a a tableview and on top of that is a searchBar. On selection of any tablerow I am dismissing the controller back to ControllerA. The problem is it shows some Bar on controllerA and I don't know how to hide it.If I don't search anything and Press the back Button on controller B then no navigationBar appears on the ControllerA. But If I select something then it shows. I think its something to do with the searchBar Presentation. Here is my code
ControllerB
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Search City"
self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredKeys.count
} else {
return dict.count-1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
// let key = self.filteredKeys[indexPath.row]
//let dictionary = self.dict[key] as! NSDictionary
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["state_name"] as? NSString)
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_country_name"] as? NSString)
if (cityName !== "-" || shortName !== "-"){
cell.stateNameLabel.text = stateName as? String
cell.cityNameLabel.text = cityName as? String
cell.shortNameLabel.text = shortName as? String
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let id = Int((((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["id"] as?NSString)! as String)
let cityName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)! as String
let countryShortName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["short_country_name"] as?NSString)! as String
delegate.country(id!,cityName: cityName, countryShortName: countryShortName,departureOrArrivalSegue: departureOrArrivalSegue!)
self.navigationController!.popViewControllerAnimated(true)
}
You can just use
self.navigationController?.setNavigationBarHidden(true, animated: true)

UISearchBarController "works", but doesn't display on screen

I have a CoreData project written in Swift that I've added a UISearchController to. I set breakpoints to see if the search is working and I can print out a list of filteredObjects from lldb in the console, but they don't display on the view.
Basically, the cells in my app load fine when the the searchBar isn't in use, but the moment I type something in the searchBar, I've verified the searchPredicate's being set and objects are being added to my filteredObjects array, but I can't figure out why they're not going on the screen. I think my problem "lives" here:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if searchPredicate == nil {
self.configureCell(cell, atIndexPath: indexPath)
} else {
// configure the cell based on filteredObjects data
if let note = self.filteredObjects?[indexPath.row] {
cell.textLabel?.text = note.noteTitle
}
return cell
}
If there's no predicate, it's cellForRowAtIndexPath is returning what it finds in the managedObjectContext. If there IS a searchPredicate, I need to return a the info from the Note object in the filteredObjects array from the searchResultsController.
I think I know "what" I need to do, but I don't know the "how" part. I'm having trouble figuring out how to do this. Thanks for reading. If you've got any ideas, I would be grateful.
Here's my Note object:
class Note: NSManagedObject {
#NSManaged var dateCreated: NSDate
#NSManaged var dateEdited: NSDate
#NSManaged var noteTitle: String
#NSManaged var noteBody: String
}
Another potential "problem area" could be my configureCell method:
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
cell.textLabel!.text = object.valueForKey("noteTitle")!.description
}
I've got the numberOfRowsInSection set here:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.searchPredicate == nil {
let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
} else {
return filteredObjects?.count ?? 0
}
}
Here are the relevant properties I've got setup and the viewDidLoad:
class MasterViewController: UITableViewController,
NSFetchedResultsControllerDelegate, UISearchControllerDelegate,
UISearchResultsUpdating, UISearchBarDelegate
// Properties
var detailViewController: DetailViewController? = nil
var managedObjectContext: NSManagedObjectContext? = nil
// Properties for UISearchController
var searchController: UISearchController!
var searchPredicate: NSPredicate?
var filteredObjects : [Note]? = nil
Here's my viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = self.editButtonItem()
if let split = self.splitViewController {
let controllers = split.viewControllers
let context = self.fetchedResultsController.managedObjectContext
let entity = self.fetchedResultsController.fetchRequest.entity!
self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController
}
// UISearchController setup
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController?.searchBar
self.tableView.delegate = self
self.definesPresentationContext = true
}
This delegate method gets called when the search bar's text the search bar becomes first responder.
// MARK: - UISearchResultsUpdating Delegate Method
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = self.searchController?.searchBar.text // steve put breakpoint
println(searchController.searchBar.text)
if let searchText = searchText {
searchPredicate = NSPredicate(format: "noteBody contains[c] %#", searchText)
filteredObjects = self.fetchedResultsController.fetchedObjects?.filter() {
return self.searchPredicate!.evaluateWithObject($0)
} as! [Note]?
self.tableView.reloadData()
println(searchPredicate)
}
}
When the searchBar is cancelled, everything goes back to normal when I use this delegate method to set the searchPredicate & filteredObjects back to nil
// MARK: - UISearchBar Delegate methods
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
updateSearchResultsForSearchController(self.searchController)
}
func didDismissSearchController(searchController: UISearchController) {
println("didDismissSearchController")
self.searchPredicate = nil
self.filteredObjects = nil
self.tableView.reloadData()
}

Resources