Use of the search box Uisearchcontroller (iOS8.0 replacement Uisearchbar + uisearchdisplaycontroller)

Source: Internet
Author: User
<span id="Label3"></p><p><p>1.searchResultsUpdater: set up a controller that displays search results</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces">    </code><code class="cpp plain">_mySearchController.searchResultsUpdater = self;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>2.dimsBackgroundDuringPresentation: sets whether the background is displayed when the search starts</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces">    </code><code class="cpp plain">_mySearchController.dimsBackgroundDuringPresentation = NO;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>3.[searchbar sizetofit]: Setting the Searchbar position Adaptive</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces">    </code><code class="cpp plain">[_mySearchController.searchBar sizeToFit];</code></td> </tr> </tbody> </table><p><p></p></p><p><p>4. Set the head view of Searchbar to UITableView</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces">    </code><code class="cpp plain">self.myTableView.tableHeaderView = self.mySearchController.searchBar;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>5.UISearchResultsUpdating: Proxy method</p></p><p><p>#import "SearchViewController.h"</p></p><p><p>@interface Shareviewcontroller () <UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate></p></p><p><p>@property (nonatomic, Strong) UITableView *mytableview;</p></p><p><p>@property (nonatomic, Strong) Nsmutablearray *visablearray;//visible</p></p><p><p>@property (nonatomic, Strong) Nsmutablearray *filterarray;//filter</p></p><p><p>@property (nonatomic, Strong) Nsmutablearray *datasourcearray;</p></p><p><p>@property (nonatomic, Strong) Uisearchcontroller *mysearchcontroller;</p></p><p><p></p></p><p><p>@end</p></p><p><p></p></p><p><p>@implementation Searchviewcontroller</p></p><p><p></p></p><p><p>-(void) Viewdidload {</p></p><p><p>[super viewdidload];</p></p><p><p></p></p><p><p>[self initial];</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) initial{</p></p><p><p>Self.datasourcearray = [nsmutablearray array];</p></p><p><p>Self.filterarray = [nsmutablearray array];</p></p><p><p>for (int i = 0; I <; i++) {</p></p><p><p>for (int j = 0; J < 4; J + +) {</p></p><p><p>NSString *str = [nsstring stringwithformat:@ "%c%d", ' A ' +i, j];</p></p><p><p>[self.datasourcearray addobject:str];</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>Self.visablearray = self.datasourcearray;</p></p><p><p></p></p><p><p>Self.mytableview = [[uitableview alloc] initWithFrame:self.view.bounds style:uitableviewstyleplain];</p></p><p><p>_mytableview.delegate = self;</p></p><p><p>_mytableview.datasource = self;</p></p><p><p>[self.view addsubview:_mytableview];</p></p><p><p></p></p><p><p>Self.mysearchcontroller = [[uisearchcontroller alloc] initwithsearchresultscontroller:nil];</p></p><p><p>_mysearchcontroller.searchresultsupdater = self;</p></p><p><p>_mysearchcontroller.dimsbackgroundduringpresentation = NO;</p></p><p><p>[_mysearchcontroller.searchbar sizetofit];</p></p><p><p></p></p><p><p>Self.myTableView.tableHeaderView = self.mySearchController.searchBar;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(nsinteger) tableView: (uitableview *) tableView numberofrowsinsection: (nsinteger) section{</p></p><p><p>If (!_visablearray | | _visablearray.count = = 0) {</p></p><p><p>_visablearray = _datasourcearray;</p></p><p><p>}</p></p><p><p>Return _visablearray.count;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) indexpath{</p></p><p><p>UITableViewCell *cell = [tableView dequeuereusablecellwithidentifier:@ "identifier"];</p></p><p><p></p></p><p><p>If (!cell) {</p></p><p><p>Cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@ "identifier"];</p></p><p><p>}</p></p><p><p></p></p><p><p>Cell.textLabel.text = [_visablearray objectAtIndex:indexPath.row];</p></p><p><p></p></p><p><p>Return cell;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) updatesearchresultsforsearchcontroller: (uisearchcontroller *) searchcontroller{</p></p><p><p>NSString *filterstring = searchController.searchBar.text;</p></p><p><p></p></p><p><p>Nspredicate *predicate = [nspredicate predicatewithformat:@ "self contains [c]%@", filterstring];</p></p><p><p></p></p><p><p>Self.visablearray = [nsmutablearray Arraywitharray:[self.datasourcearray filteredarrayusingpredicate:predicate]];</p></p><p><p></p></p><p><p>[self.mytableview reloaddata];</p></p><p><p>}</p></p><p><p></p></p><p><p>Use of the search box Uisearchcontroller (iOS8.0 replacement Uisearchbar + uisearchdisplaycontroller)</p></p></span>

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.