How to add a search bar to a collection view controller? - ios

How do you add a search bar to a collection view controller in iOS 12? I realize this should be a simple question but I'm having difficulty finding an example that doesn't look half-baked.
This can either be just using a search bar or a search controller. I don't know what the best approach should be in iOS 12. I would also like to do this programmatically.

Use a UISearchController. Put the collection view controller into a navigation controller and set the searchController of its navigationItem. Now the search bar will appear in the navigation bar. You can configure things so that the search bar appears only when the user scrolls the collection view all the way down; that is effectively the same as making the search bar be at the top of the collection view. This is a nice interface and is what Apple expects you to use.

I am a bit confused, it is a very strange thing to put the UISearchBar in the UICollectionView. Because the UICollectionView the display uses the UICollectionViewCell, each UICollectionViewCell can be reused.
So I guess you are not going to put in the UICollectionView, but search the results through the SearchBar, using the UICollectionView to display the results.
Sorry, my network can't upload images. The layout is probably UISearchBar and UICollectionView in UIViewController.
- UIViewController
- UISearchBar
- UICollectionView
In the UISearchBarDelegate proxy method, the network request can be executed when the search is triggered, and the data returned by the network request is refreshed on the list.
If you are not very demanding on the layout of the list, you can use UITableView, UICollectionView needs to set UICollectionViewLayout to be slightly more complicated.

In code huh? Using storyboards all you need to do is add the search bar and connect it to an IBAction outlet. When it changes you pass the value to a predicate and reload your collection. Delegate and other Mac Search bar options can be set in code or right in the storyboard. Sorry I couldn't fully bake it for you!

Related

How to segue from xib file uiview to storyboard ViewController

My program does not have an explicit table. It’s only table comes from “Search Bar and Search Display Controller” view as searchDisplayController.searchResultsTableView. So to create it’s custom TableViewCell, I use a xib file. Now I need to segue from the custom cell defined in the xib file to a ViewController defined in the storyboard? How do I do that?
stated differently: How to segue from a custom TableViewCell of “searchDisplayController.searchResultsTableView” to a ViewController
back story How do I add storyboard-based Header and CustomTableCell to a “Search Bar and Search Display Controller”
There seems to be an answer already but I don't get it: Segue from Storyboard to XIB
As with any table view, you should be able to set the UITableViewDelegate and UITableViewDatasource delegates on the searchDisplayControl class. And then handle segue event in the didSelectRowAtIndexPath method. I've never played with search display controllers as I typically break apart search bar and table views when I do anything search related for the sake of flexibility, and how you're saying you want to use prototype table view cells in a tableview within the storyboard.
But, if you're set on using the searchDisplayController, you could possibly get away with something like this..
You can't segue from one XIB -> Storyboard, or between 2 different storyboards.
You can pass a reference to the viewController to each tableViewCell, and access the segue that way if you'd like. However, I'd probably use protocols/delegates personally in this situation for the sake of future flexibility.
As we discussed in comments above, it's better to avoid UISearchDisplayController since it's deprecated in iOS 8.
So it would be better to use something like that https://github.com/alexbutenko/SimpleUITableViewSearch
You can use self.navigationItem.titleView = searchBar to embed search bar into navigation bar. If you have any issue just check code sample.

Implementing A Mail app-like User Interface

I am trying to implement a user interface that is similar to the iPhone's Mail app.
The main screen displays a table. From the table the user can select a cell, at which point the next screen is launched. At the bottom, there is a bar showing a short text and an icon.
The second screen displays the details of the cell. It will also be a table display. The bottom bar shows icons associated to this screen.
What kind of layouts do I use to implement this in Xcode?
1. Do I use a View controller, add a View and embed a TableView and a Toolbar inside that view?
2. Do I use a Table View Controller and add a Table View inside it and use the bottom tool bar that comes with the table view?
In the Table View Programming Guide for iOS, under 'Recommendations for Creating and Configuring Table Views,' it says 'Use an instance of a subclass of UITableViewController to create and manage a table view.' When I use this, the bottom bar can only be fixed or disappear when going back and forth between two screens via segue. That makes me wonder whether I should just use a View controller which is against the recommendation.
Use a UIViewController
Why?
If you are going to display more than a tableView then it is recommended to use a UIViewController for the storyboard.
UIViewController are much more flexible. I guess your confusion comes from the following documentation line:
Use an instance of a subclass of UITableViewController to create and
manage a table view
This does not mean you must to drag and drop a UITableViewController on the storyboard. It means your class need to inherit from UITableViewController or at least implement the deleguate and datasource methods.
You would use UITableViewController only and only if you need to display a tableView.

UISearchBar acting like a UITableView Cell

I have a UISearchBar implemented into my UITableViewController, my problem is when I scroll down in my table view, the search bar acts like a cell and scrolls with the table, therefore disappearing. Is there a war to programmatically fix this?
I know there are similar questions on SO with this issue and I have exhausted all the resources provided in those posts.
Thanks
Sure, don't use an UITableViewController but use a normal UIViewController with UITableView and UISearchBar inside.
When you use an UITableViewController, its UIView is directly the UITableView. So every control and subview go in the tableView. For this reason you need to change strategy and use the way above.

Search bar and search display controller without table view

I was wondering if it was possible to have a search bar and search display controller without having a table view on the same view. If this is possible, I would really appreciate it if you would please respond.
It works just fine without a table view. The UISearchDisplayController instance needs a searchResultsDatasource and delegate, which are really UITableView datasource and delegate.
Since these implementations are present in UITableViews, they are often reused as the implementations for the search controller, but that needn't be the case.

How to use ajax with search bar to populate table view in iPhone?

So I'm really new to iPhone development and this is what I want to achieve. I want a search bar that uses a REST api that I've written to get back data and populate a table view.
In terms of what I've attempted - I'm not sure where to start, so I'll just post some thoughts.
What I've done is created three views - a root view, a search view and a results view. The root view should have a UISearchBarDelegate, an UITableView DataSource and an UITableViewDelegate because there needs to be a controller that can execute the search and populate the table.
However I'm not sure if this is the right approach, or even where to go next after this.
I know I should probably try more code, but I'm just really not sure where to start because this is an architecture thing - I'm not sure where the data will be passed between search and results(I think this should be in the root view) and how that root view will know when the search is done.
Thanks
You can do this all in one ViewController. Make it a delegate to the UITableView and the UISearchBar, as well as the DataSource for the tableview. Put the searchbar in a UINavigationBar along with a button with the text "Search". When the button is pressed take the text in the search, send it to your REST api, then set the data as the DataSource. Then just call [tableView reloadData] and the view will refresh with your updated data.

Resources