Cocoa - How to make a search field for a tableView? - ios

I have a table with several results, and now I want to make a customized search field (just a box with a text label). And I want that the table results instantly match what the user types on the search field.
Example: I tap "AN" and the table results are "Ana Fernandes", "Ana Alicia", "Ananás", etc.
Can you help me with that?

You will have a UIViewController with a normal view (ie self.view). Add a UISearhBar as a subview of self.view and put its origin at 0,0. Add a UITableView as a second subview, and set it's origin to (0, searchBar.frame.size.height). Now you have both views you want. Add yourself as a delegate to the searchbar, and implement the delegate methods so you can see what the search string is as the user enters text. You will then fuss with your tableview to just show the cells you want.

Related

First button of UITableView as Search as well as Add textfield

If you see the screenshot below, its a view from iOS Files app which lets you select tags for a file.
I want to make a similar Tableview which lets user either select existing items or add new items. But I also want the add textfield to be a search field. That is, when the user starts typing there, he also gets search results from the existing items.
The way I am planning to implement this is...
Two table views.
One has a static cell to 'Add' and is not vertically scrollable.
Right underneath is the second table view with existing itmes, which is vertically scrollable.
when the user taps and starts typing in the textfield, there is a search side by side, in the second table view and the user can select an item from it as well.
When there are no matching results, an add button is highlighted and the user can add a new entry.
I want to know what is the best way one can implement such behaviour?
Also,
(In terms of UI Design)
Should one make use of the UISearchBar instead of a textfield?
Or, is it better to simply use the UISearchController?

UISearchController: single vs separate tableview for display and search

I need to have a search bar for a table view that displays a list of data, not sure whether I should use one table view for both displaying and searching, or have separate table views for display and search respectively.
Single table view:
+ feels easier to implement by just switching data source
- extra work if I want to maintain the scroll position (after searching) of the display table view
Separate table views:
+ no extra work needed to preserve scroll offset of the display table view
- extra work to switch between display and search modes
Is there anything critial I missed? What is the recommended way?
Update:
I need the search bar to stick on top, so it can't be the tableHeaderView of the table view (which scrolls when table view scrolls), or section header view because I've got different sections.
Thanks!
I've used a single table view and used a read only property to filter the array of items based on the search text.
I stored the original data in Property X and based on the text entered in the search bar i filtered the data returning by the Property (readonly) Y and used as the table data source.
Using the search bar delegate method you can then filter the contents of the table as the user enters text into the search bar.
The first one is the recommended way to implement the searching functionality. Managing the datasource is easier then to handle the two different tableviews.
You can try for UISearchController which provides the searchBar with a TableView and can have delegate methods to handle the case.
Here and here are some good tutorials to do the same.

iOS : How to display search results in another table view ? And how to show suggestions while searching?

Currently, what I am showing search result same tableview.
Now, I want to show results in another table view.
Here is one example of search & suggestion.
Please drag another tableview in view and when you are going to search then display that data in upper table and hide and show the upper table on the basis of search. Also create an outlet of upper tableview and apply condition in TableViewDelegate and DataSource.

Dealing with keyboard and tableviewcell

The layout for one of my View Controllers is such: I have a scroll view embedded inside my VC. Inside my scroll view, I have a table view that consists of 5 cell. The first 3 cells consist of a textfield thats pulls its text from a dictionary and this changes depending on certain situations. These textfields are editable and so tapping on them brings up the keyboard, the issue however is that I would like my view to scroll when I tap on the text field because right now they keyboard hides the the third editable text field. Another issue is that at the moment, clicking outside teh table view doesnt cause the keyboard to be dismissed and so the only way of dismissing the keyboard is tapping on the return key. What I would like to happen is that when I tap on either one of the 3 editable fields, the scroll view ought to scroll up a certain number that I define (this is so that I can define how much to scroll depending on which row is currently selected). One of the issues I'm facing is that I can't directly reference these textfields in my VC since they're all created through one prototype cell. My thinking was that I could create a dictionary with the 3 textfields as keys and then the scrollview y coordinates as values and then use that. However , I wasn't sure how to do this in my situation (with the prototype cells). Would really appreciate if someone could show me some sample code on how to do this.
You can reference your text fields by calling cellForRowAtIndexPath: to get the UITableViewCell, then calling viewWithTag: to get your UITextField. Just assign the text fields a tag number. Also, set the text field's delegate to be your view controller so that you can respond to a user tapping to edit text.

How to show keyboard programmatically

I have UITableView where each cell consists of two UILabel, I want to show up keyboard when the cell is selected? Is it possible with UILabels?
If you just want to pop up a keyboard, you can add a tiny invisible (transparent 1x1 with transparent text) UITextField anywhere in any visible view and make this text field first responder to pop up a keyboard. Then you can redirect the input text to any of the two labels (or somewhere else) using the text field delegates to capture the input.
Yes, the label has to conform to the UIKeyInput protocol. Note that this is an either-or proposition. If the label conforms to UIKeyInput, then when it becomes first responder, the keyboard will be displayed, whether you want it or not.
I'm not sure how you mean this exactly since it is obviously not possible to edit two textfields for labels at the same time. Hence the following assumes you want to show the text in your cell using UILabel, but want to be able to edit the cell's text.
You can't directly use the keyboard to edit UILabels. The easiest solution is to directly use UITextFields instead of the UILabels.
An alternative is to have both a UITextField and UILabel in the cell. Then show the textfield (by settings itsß hidden property toYES`) when the cell is selected and hide the label. When editing is finished, do the reverse (i.e. showing labels, hiding textfields).
To show the keyboard directly after selecting the cell you can call [someTextField becomeFirstResponder];. To check if the user is done editing (and e.g. tapped the return key), you can set the delegate of the UITextField.

Resources