The use of Uisearchcontroller. (ios8+)

Source: Internet
Author: User

This method has long been found, but the unanimous useless, today brought to use, found some problems.

1, this thing and the table view is very convenient to use, first of all, create a new project, the table view controller as the root view of the project, and add a navigation (of course, you can not do so, but your search controller to use with the table view)

2, @interface Tableviewcontroller () <uisearchcontrollerdelegate,uisearchresultsupdating>, here is the two agents to use,

3.

@property (Nonatomic,strong) Nsarray *content;//is here to store your data.

@property (Nonatomic,strong) Nsarray *searchresult;//This is the result of the search.

@property (Nonatomic,strong) Uisearchcontroller *searchcontroller;

4, the proxy settings here and some properties (is called the property bar) initialization operation,,, note that the initialization of the search controller is placed before the proxy settings.

-(void) Viewdidload {

[Super Viewdidload];

self.content = @[@ "Beijing",

@ "Shanghai",

@ "Guanghzou",

@ "Shenzhen",

@ "Huhuhu"

];

Self.searchresult = @[];

Self.searchcontroller = [[Uisearchcontroller Alloc]initwithsearchresultscontroller:nil];

Self.searchController.searchResultsUpdater = self;

Self.searchController.delegate = self;

[Self.searchController.searchBar SizeToFit];

Self.tableView.tableHeaderView = Self.searchController.searchBar;

}

5, here is the implementation of the Proxy method (only this must be achieved, that is, this refresh, this method of the trigger event is what)

#pragma Mark Uisearchresultsupdating

-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) searchcontroller{

if (searchcontroller.searchbar.text.length>0) {

Self.searchresult = [self searchByText:searchController.searchBar.text];

}else{

Self.searchresult = self.content;

}

[Self.tableview Reloaddata];

}

The search results are returned in an array, which is the judgment of the search results.

-(Nsarray *) Searchbytext: (NSString *) text{

Nsmutablearray *result = [Nsmutablearray array];

For (NSString *str in self.content) {//traverse the array where you store all the data

if ([[str lowercasestring]rangeofstring:[text lowercasestring]].location! = nsnotfound) {//This method is used for the first time, thisNSPredicate有什么区别

[Result Addobject:str];

}

}

return result;

}

6, then the table view of 3 Ask 1 answer

#pragma mark-table View data source

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {

return 1;

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

if (self.searchController.active) {//here can return different values depending on whether the search controller is active or not, the table view returns a few rows of the search results if it is a search state, and returns the number of results if it is not in the search state.

return self.searchResult.count;

}else{

return self.content.count;

}

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "cell" forindexpath:indexpath];

if (self.searchController.active) {//The logical same return row number here

Cell.textLabel.text = Self.searchresult[indexpath.row];

}else{

Cell.textLabel.text = Self.content[indexpath.row];

}

return cell;

}

Also, how do I implement a click event on a search result?

The use of Uisearchcontroller. (ios8+)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.