Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create search field in Tableview

I am creating an application with a table view, which has a large amount of data in it. Because of this, it is necessary for me to use a search field.

Does anyone have any idea how to create a search-option in a tableview?

like image 426
sinh99 Avatar asked May 11 '11 11:05

sinh99


People also ask

How do I add a search bar to UITableView?

Adding Search Bar on TableView Add the UISearchBar at the top of your UITableView. … and give the name searchBar. In the ViewController add a Boolean called searching to know when to switch between the full list of items and the list of items from the searching results.

How do I search in Swift?

You can add a UISearchBar as you would with any other control by dragging one to your view controller in interface builder or by programmatically adding it. The delegate property of search bar must be set to an object that implements UISearchBarDelegate.

How do I customize the search bar in Swift?

Change Search Bar Default Image Color The left hand default search image in UISearchBar represents the left view of the UITextField. The Image is rendered to change it to the desired colour. @IBOutlet weak var searchBar: UISearchBar! Hope it will help you in customising the UISearchBar in your app.


1 Answers

Use the UISearchBar property, declare it using

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,30)];

and add it to your UIViewController's view as a subview.

After that, use the searchbar delegate methods in your view controller:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

Check out this tutorial to get a hang of the search bar and its delegates!

EDIT: This method isn't about using a searchbar in tableview itself, but above it. That means you have to put tableView as a subview of a UIViewController, and the searchBar as a subview of the same UIViewController as well!

like image 99
Joetjah Avatar answered Nov 06 '22 23:11

Joetjah