Search function, basic per app standard.
It's easy to implement, but after iOS8 Apple recommends using Uisearchcontroller, the official demo:table Search with Uisearchcontroller
The actual development of the basic also is to use the old Uisearchdisplaycontroller+uisearchbar program, because to take care of some version of the low user.
Found that the time has not written all forgotten almost, leisure, together with the next, easy to read.
This article begins with the Uisearchdisplaycontroller first. Relatively simple, it is not wordy, directly affixed to the code.
- UITableView steps are omitted.
-
Create a new uisearchbar
1 _searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0 , 0 , Self.view.frame.size.width, 44 )]; 2 _searchbar. delegate = self; 3 _searchbar.placeholder = < span> search ; 4 _searchbar.backgroundcolor = [Uicolor clearcolor];
-
according to searchbar Create a new
1 _ Searchdisplayctl = [[Uisearchdisplaycontroller Alloc] Initwithsearchbar:_searchbar Contentscontroller:self]; 2 _searchdisplayctl. delegate = self; 3 _searchdisplayctl.searchresultsdatasource = Self; 4 _searchdisplayctl.searchresultsdelegate = self;
-
set tableview Tablehearderviewsearchbar
1 _tableview.tableheaderview = _searchbar;
-
Change The Cancel button for Searchbar to " cancel ", note: Because iOS7 and previous hierarchies are different,iOS7 Before traversing once can get,iOS7 need to traverse two times, in the proxy method of Uisearchbar, set the
1- (void) Searchbartextdidbeginediting: (Uisearchbar *) Searchbar2 {3 for(IDSviewinchsearchbar.subviews)4 {5 if([Sview Iskindofclass:[uibuttonclass]])6 {7[(UIButton *) Sview Settitle:@"Cancel"Forstate:uicontrolstatenormal];8 Break;9 }Ten for(IDSsviewinch[Sview subviews]) One { A if([Ssview Iskindofclass:[uibuttonclass]]) - { -[(UIButton *) Ssview Settitle:@"Cancel"Forstate:uicontrolstatenormal]; the } - } - } - + //by default, the Cancel button appears as soon as you have entered the search state. - //but do not know why sometimes do not come out, rigorous, we add the following paragraph ourselves. + [Searchbar setshowscancelbutton:yes animated:yes]; A}
- The final step is to search for the results. Processing in Uisearchdisplaycontroller Agent
1 #pragmaMark-uisearchdisplaycontroller2-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) SearchString3 {4 [_resultlist removeallobjects];5 6 for(NSString *substringinch_datalist)7 {8 if([subString rangeofstring:searchstring].location! =nsnotfound)9 {Ten [_resultlist addobject:substring]; One } A } - - returnYES; the}
The simplest of a search completed, Uisearchbar and Uisearchdisplaycontroller also provide us with a lot of other agent methods, are some of the regular reading of the meaning of the type, do not introduce each, the actual use to look at the next can be. The Apple package is already good, and the UI is pretty, so we don't have to write one ourselves.
However, there are two points that may be used in the actual process. Online also quite a lot of information, probably sorted under. Easy to find and use later.
- Change the background of a Uisearchbar
There may be a need for us to change the background color or background image of the project, and its approximate implementation principle is similar to changing the title of the Cancel button. Need to be aware of different versions of iOS, different hierarchies
Uisearchdisplaycontroller search (iOS8 ago)