Using UISearchController to search a UITableView in a different View Controller - ios

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!

Related

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.

Search Bar controller in TableView

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

UISearchController is appearing into nextView with swift

I have implemented a simple UISearchController into my UITableViewController programatically. And below is my complete code:
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
let appleProducts = ["Mac","iPhone","Apple Watch","iPad"]
var filteredAppleProducts = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if (self.resultSearchController.active){
return self.filteredAppleProducts.count
}else{
return self.appleProducts.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell?
if (self.resultSearchController.active)
{
cell!.textLabel?.text = self.filteredAppleProducts[indexPath.row]
return cell!
}
else
{
cell!.textLabel?.text = self.appleProducts[indexPath.row]
return cell!
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("Detail") as! DetailViewController
self.navigationController!.pushViewController(vc, animated: true)
}
func updateSearchResultsForSearchController(searchController: UISearchController){
self.filteredAppleProducts.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = (self.appleProducts as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredAppleProducts = array as! [String]
self.tableView.reloadData()
}
}
My resultSearchController is working completely fine but when I click on any cell then UISearchController is appearing into my next view too as shown into below image:
But my Second view is empty.
how I can remove this resultSearchController when I switch to another view so it will not appear into next view?
Project Sample for more Info.
Just add one line in viewDidLoad
self.definesPresentationContext = true
Doc
A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.
GIF

How can I make my UISearchController show the cells underneath?

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?
If the searchText is empty, then set the filteredTableData to items
extension TableViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
var filteredArray : [TextCell] = [TextCell]()
if searchController.searchBar.text == "" {
filteredArray = self.items
} else {
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()
}
}
What you can do is populate the filteredDataArray with no filtering at first, and then apply the filtering as it goes. You aren't setting filteredDataArray to anything until you start searching, so in the start of the search just set it to the full data set.
Bonus:
You can use tableViewDidSelectRowAtIndexPath to catch selection, and this should work even with the search table view.

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