1.UISearchBar (the effect is as follows:)
① Creating Uisearchbar Objects
Initialize, define frame Uisearchbar *bar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0, 50, Self.view.frame.size.width)]; Add to the view of the controller [Self.view Addsubview:bar];
properties of the ②uiserachbar
Autocapitalizationtype: contains 4 types, but sometimes the keyboard blocks this genus. 1.autocapitalizationType ———— automatically set the casing of the input text object. bar.autocapitalizationtype = uitextautocapitalizationtypewords; //2.autocorrectiontype ———— Automatic error correction of input text objects bar.autocorrectiontype = Uitextautocorrectiontypeyes; 3. Set Title Bar.prompt = @ "All contacts"; 4. Set color bar.tintcolor = [Uicolor purplecolor];//render color bar.bartintcolor = [Uicolor orangecolor];//Search bar Color Bar.backgroundcolor = [uicolor purplecolor];//background color Because of the frosted glass effect (transulent). 5.translucent ———— Specifies whether the control will have a perspective bar.translucent = YES; 6.scopeButtonTitles (range buttontitle) bar.scopebuttontitles = @[@ "Exact search", @ "Fuzzy search"]; Bar.selectedscopebuttonindex = 1;//Specifies the default selection of the selection bar//7 by subscript. Controls whether the selection bar in the lower part of the search bar is displayed (requires set to Yes to use Scopebar) Bar.showscopebar = YE S 8. Set the button to the right of the search bar bar.showssearchresultsbutton = yes;//down arrow Bar.showscancelbutton = YES; Cancel button Bar.showsbookmarkbutton = YES; Bookmark button//9. Hint Content baR.placeholder = @ "Search"; 10. Cancel the keyboard operation [Searchbar resignfirstresponder]; //11. Set proxy//uisearchbar do not perform search behavior, you must use delegate when you enter search text, When you click the button, the agent's method will complete the search for the corresponding action. . The delegate of the control, the delegate to comply with the Uisearchbardelegate protocol, the default is nil bar.delegate = self;
protocol method for ③ agent to try
1). Enter Edit Event Handling
–searchbar:textdidchange:–searchbar:shouldchangetextinrange:replacementtext:–searchbarshouldbeginediting:– Searchbartextdidbeginediting:–searchbarshouldendediting:–searchbartextdidendediting:
2). Click button Event Handling
–searchbarbookmarkbuttonclicked:–searchbarcancelbuttonclicked:–searchbarsearchbuttonclicked:– Searchbarresultslistbuttonclicked:
3). Click the Scope button event handling
–searchbar:selectedscopebuttonindexdidchange:</span>
2.UISearchDisplayController (Note: iOS8 above has been deprecated)
Combined with the Uisearchbar implementation of the following, to achieve the search function.
Tip: Detect the Xcode system version code as follows:
[[[Uidevice Currentdevice] systemversion] floatvalue] >= 8.0? Yes:no;
①. Creating objects
The Uisearchbar object needs to be created, where the object is defined as a property Self.searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0, 0, 320, 40)]; Set the Uisearchbar property, which has uisearchbar details . self.searchBar.placeholder = @ "Enter province name"; & nbsp; Self.searchBar.autocorrectionType = uitextautocorrectiontypeno; Self.searchBar.autocapitalizationType = uitextautocapitalizationtypeallcharacters; Self.searchBar.scopeButtonTitles = [Nsarray arraywithobjects: @ "All", @ "A", @ " B ", @" C ", @" D ",nil]; Self.searchBar.showsScopeBar = yes; Self.searchBar.keyboardType = uikeyboardtypenamephonepad; Self.searchBar.showsBookmarkButton = YES; Seachbar as the controller perspective, view controller, inherit uitableviewcontroller self.tableView.tableHeaderView = _searchbar; Add Uisearchbar to Uidsearchdispalycontroller self.displaycontroller = [[Uisearchdisplaycontroller alloc] initwithsearchbar:_searchbar Contentscontroller:self];
Note: Searchbar ———— after Searchdisplaycontroller initialization, Searchbar is not modifiable and is a readonly attribute.
② Configuring the properties of Uisearchdisplaycontroller
Active ———— is the search interface visualization, the default is no, the SetActive method can be set. Self.displayController.active = YES; Searchresultsdatasource ———— Data Source for search results, proxy object (Uitableviewdatasource) Self.displayController.searchResultsDataSource = self; Searchresultsdelegate ———— a delegate to search results, subject to agreement uitableviewdelegate self.displayController.searchResultsDelegate = Self
③ Implementation
/*searchdisplaycontroller itself has a searchresultstableview, so when performing the operation of the first to determine whether the search results tableview, if it is to display the results of the search data, if not, it is TableView's own view, and the original data needs to be displayed. * /if (TableView = = Self.tableview) { return self.dataArray.count; } else { nspredicate *predicate = [ Nspredicate Predicatewithformat: @ "self contains [CD]%@", Self.searchBar.text]; Self.arr = [[Nsmutablearray alloc] Initwitharray: [Self.dataarray filteredarrayusingpredicate: predicate]]; return self.arr.count; }
The ④ uses the Uisearchdisplaydelegate delegate method for the search operation:
1). Search status Change event handling method:
–searchdisplaycontrollerwillbeginsearch:–searchdisplaycontrollerdidbeginsearch:– Searchdisplaycontrollerwillendsearch:–searchdisplaycontrollerdidendsearch:
2). Loading and unloading TableView event handling methods:
–searchdisplaycontroller:didloadsearchresultstableview:–searchdisplaycontroller: Willunloadsearchresultstableview:
3). Show and hide TableView event handling methods:
–searchdisplaycontroller:willshowsearchresultstableview:–searchdisplaycontroller:didshowsearchresultstableview : –searchdisplaycontroller:willhidesearchresultstableview:–searchdisplaycontroller: Didhidesearchresultstableview:
4). Response event handling when search conditions change:
–searchdisplaycontroller:shouldreloadtableforsearchstring:–searchdisplaycontroller: Shouldreloadtableforsearchscope
3.UISearchController (iOS8 new features)
The Uisearchcontroller implementation is basically consistent with the above effects, and is suitable for iOS8 or later versions
Implement such as Search effect
The code is as follows:
1) New controller, inheritance and Uitableviewcontroller, defining attributes in extension
Store the original data @property (nonatomic, retain) Nsarray *dataarr;//store The retrieved data @property (nonatomic, retain) Nsarray *arr;
2). Load data, lazy load
-(Nsarray *) Dataarr { if (!_dataarr) { Self.dataarr = [Nsarray arraywithobjects: @ "Allan", @ "abbbb", @ " ACCCC ", @" BCCCCC ", @" CDDDDFFK ", @" cddkllll ", @" EKKFLFL ", @" EKLJLJFG ", @" Leslie ", @" Mm ", @" MEINV ", @" Meihi ", @" Catilin ", @" Arron ", @" 211 ", @" 232 ", @" 243 ", @" at ", @" 285 ", @" 106 ", @" 311 ", @" 432 ", @" 543 ", @" 664 ", @" 785 ", @" 806 ", nil]; } return _dataarr;} If the retrieved data is empty, the original data is assigned to the retrieved data-(Nsarray *) arr {if (!_arr) {Self.arr = Self.dataarr; } return _arr;}
3. Loading Uisearchcontroller objects
-(void) viewdidload { [super viewdidload]; Cell reuse mechanism, invoking the system's [Self.tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "lock"; Create a search bar, set itself as the controller that will show the results Uisearchcontroller *SEARCHVC =[[uisearchcontroller alloc] Initwithsearchresultscontroller:nil]; Set rendering color SearchVC.searchBar.tintColor = [Uicolor orangecolor]; Set the status bar color searchVC.searchBar.barTintColor = [Uicolor orangecolor]; Sets whether the background is displayed when the search starts (very important) searchvc.dimsbackgroundduringpresentation = NO; Adapts to the entire screen [Searchvc.searchbar SizeToFit]; Set the controller that displays the search results searchvc.searchresultsupdater = self;//Protocol (uisearchresultsupdating)//Set the search bar of the searching controller to the header view Self.tableView.tableHeaderView = Searchvc.searchbar;}
4). To implement the methods in the protocol, you must implement
-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller { //predicate detection Nspredicate *predicate = [nspredicate predicatewithformat: @ "self contains [CD]%@", SearchController.searchBar.text]; Stores all and search-related content to arr array Self.arr = [Nsmutablearray arraywitharray: [Self.dataarr filteredarrayusingpredicate :p Redicate]]; Reload Data [Self.tableview reloaddata];}
5). Set other Uitabelviewcontroller in the
#pragma Mark-table View Data source//set how many partitions-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {//Return th E Number of sections. return 1;} How many rows of data per partition-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section {return self.arr.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Self.cell = [TableView dequeuereusablecellwithidentifier:@ "Lock" Forindexpath:indexpath]; Set the content displayed on the cell, that is, the data after the search Self.cell.textLabel.text = Self.arr[indexpath.row]; return Self.cell;}
Note: The above is to implement search box search empty energy. (When the search content is empty, all data returned, if the search content is empty, return empty, you need to make other modifications.)
"Learning iOS: UI series" (Uisearchbar,uisearchdisplaycontroller) and Uisearchcontroller (IOS8 new features)