Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

UISearchController Tutorial: Getting Started

In this UISearchController tutorial, you'll learn how to add quick and responsive search to a table view, including both dynamic filtering and a scope bar.


This is a companion discussion topic for the original entry at http://www.raywenderlich.com/113772/uisearchcontroller-tutorial

Thanks for tutorial. I would like to achieve something like what is done in the Contacts app built into iOS, where the search bar stays always visible.

I have added the search bar outside the table view, but when I tap on the search bar the search bar flies to the left top corner of the screen.

I’m facing a bug where I can’t see the cursor in the search bar. All else works fine though

I would like to learn for OBJ-C and iOS9 how to do searchcontroller NOT in UITableView controller but with tableView in UIViewController

I have downloaded the complete project and when run on a device (iPhone 6), there is a five to seven second lag after tapping in the empty searchbar before the keyboard appears.

I have noticed this to be the same delay when add the following code to make the keyboard active by default.

override func viewDidAppear(animated: Bool) {
    
    searchController.searchBar.becomeFirstResponder()
   searchController.active = true
}

Any ideas on why the lag and better yet how to “speed up” the keyboard loading?

Keyboard lag again…

It turns out that the keyboard lag is only evident when the iPhone is plugged in to the computer and Xcode is running the app. There is no lag when CandySearch is run as a standalone app (i.e. unplugged).

Thanks for the great tutorial!

Thanks for the excellent tutorial, Andy.

I’ve got everything working but am hoping someone can give me some pointers for implementing a feature.

If the search returns a result, I’d like to increment a label that displays the number of “hits”.

I’ve tried checking the count of the filtered array being = to 1 but that doesn’t seem to work because the array doesn’t contract as results are filtered out.

I feel like a boolean’s going to be required but not sure how/where to implement it.

I’m pretty sure I’ve got the code down for the incrementing and label.text updating but am not sure how to successfully detect a “hit”.

Any suggestions/resources/links appreciated.

Again, excellent tutorial and learned a lot!

Thanks!

Thanks for the great effort made … i need to zoom in detail view to maximize photos…how i can do that…thanks for help

I have implemented a UISearchController in a VC with a table view and a new UiTableViewController for the search results, everything works fine, the search i mean, but i’m getting a “Presenting view controllers on detached view controllers is discouraged” warning every time i present the search results.
what i’m missing?

code segment:

// Create the search results controller and store a reference to it.
self.resultsController = [[UITableViewController alloc] init];
self.resultsController.tableView.delegate=self;
self.resultsController.tableView.dataSource=self;

self.resultsController.tableView.backgroundColor=[UIColor colorWithRed:0.905 green:0.929 blue:0.882 alpha:1];
self.resultsController.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;


self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsController];
self.searchController.delegate=self;

// Use the current view controller to update the search results.
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation=NO;


// Install the search bar as the table header.
self.tablaDirectorio.tableHeaderView = self.searchController.searchBar;

// It is usually good to set the presentation context.
self.definesPresentationContext = YES;

Any Ideas?

Hello,

Thank you for this tutorial.

I would know how to add a titleForHeaderInSection for classify by alphabitcal order the candies ?

  • titleForHeaderInSection = C
    name: Chocolate Bar and category : chocolate.
    titleForHeaderInSection = D
    name: Dark Chocolate and category : Chocolate.
    etc.

Can you help me ?

Hi Andy!
Great tutorial! I would like to use the objective-c version but the link to “Original post” is broken and therefore I can’t download sample project source code of previous version.
Can you please provide the right links?

Thank you,
Shebuka.

Thanks for the Tutorial, I’ve added a Search Bar where I can search and edit the contact. Everything works fine except the search results are not showing up after I updated the contact and come back. I’ve to manually clear the text to see the updated contact.
Is there a way to clear the text from the Search bar when we return to the view or if not how to make the text active when we move back and forth, so that we can see the updated result.

I’ve encountered a problem. The app runs and when I enter the tableview with the search function (controller) everything works fine but as soon as I click on the search bar the app crashes. And the breakpoint seams to be in the filter or in the UISearchResultsUpdating extension. Would really appreciate some help here.

func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredResult = result.filter { item in 
        return item.name!.lowercaseString.containsString(searchText.lowercaseString)
    }
    listTableView.reloadData()
}

It would be great if someone would do a tutorial UISearchController for tvOS. There are a number of (confusing) differences in implementation between iOS and tvOS.

Hey folks - thanks for this tutorial - Note for Swift 3 the protocol conformance has changed slightly - took a while to find so hopefully this helps someone:
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}

from:
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}

Cool tutorial!
What if you want to make the detail image smaller and put some detailed text underneath?
Wich code to use?

Hi, I like so much this article, and I’d like to know if it’s possible to have access to the uisegmentedcontrol that it’s shown in the searchBar. I need this segmented control to have 2 lines, but I don’t know how to have access to it.

Thanks a lot.

hi sir, i have simple issue i have manager class array all data come from one time from sqlite i store in singleton var and there is the class array

like managerArray = ManagerClass

and i access all the data in the UITableView using indexes of class like:

let mgVar = managerArray[indexPath.row]
cell.textLabel.text = mgVar.title

please help me how can i perform updateSearchResult i tried so many times but not successful basically it’s not a simple array it’s class array how can i do that please it’s kind and humble request do help me out, i also asked in stackoverflow but all are juggled me. here is the stackoverflow link full code there as well:

StackOverFlow Link

Thanks.

I converted the final result to Swift 3. Here’s the GIT:

https://github.com/AZCoder2/Guess-The-Pet.git

Hi,

fist of all: Great tutorial!

Is it possible to filter the results when tapping one of the scope titles without entering a letter in the search bar?
That would be very cool. I am sure it is pretty simple to realise but I am very beginner =)

And sorry for my bad english :smiley: