{"id":263,"date":"2015-06-01T13:59:56","date_gmt":"2015-06-01T05:59:56","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=263"},"modified":"2016-07-13T16:54:33","modified_gmt":"2016-07-13T08:54:33","slug":"uisearchbar%e4%b8%8euisearchdisplaycontroller","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2015\/06\/01\/135956263.html","title":{"rendered":"UISearchBar\u4e0eUISearchDisplayController"},"content":{"rendered":"<p>1\u3001\u4e00\u5f00\u59cb\u5462\u6211\u662f\u641e\u4e0d\u6e05\u695a\u8fd9\u4e24\u4e2a\u4e1c\u897f\u6709\u4ec0\u4e48\u5173\u7cfb\uff0c\u6216\u8005\u8bf4UISearchDisplayController\u6211\u6839\u672c\u5c31\u6ca1\u7528\u8fc7\uff0c\u90a3\u4e48\u5c31\u5148\u6765\u770b\u4e0bUISearchBar\u7684\u4f7f\u7528\u6548\u679c<\/p>\n<p>2\u3001UISearchBar\u662f\u4e2a\u641c\u7d22\u5de5\u5177\u6761\uff0c\u8fd9\u4e2a\u63a7\u4ef6\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff0c\u4ed6\u4e0eUITextField\u5176\u5b9e\u6709\u70b9\u50cf\uff0c\u80fd\u591f\u76d1\u542c\u5f00\u59cb\u641c\u7d22\uff0c\u641c\u7d22\u8bcd\u53d8\u5316\uff0c\u641c\u7d22\u7ed3\u675f\u7b49\u4e00\u7cfb\u5217\u72b6\u6001\uff0c\u5e76\u8c03\u7528\u4ee3\u7406\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u529f\u80fdUITextField\u4e0d\u662f\u4e5f\u90fd\u6709\u5417\uff1f\u53ea\u662f\u4ed6\u662f\u4e13\u4e1a\u7684\u641c\u7d22\u63a7\u4ef6\uff0c\u957f\u5f97\u597d\u770b\u4e00\u70b9\u800c\u5df2\uff01<\/p>\n<p>3\u3001\u6765\uff0c\u770b\u4e0bUISearchBar\u5728TableView\u4e2d\u7684\u8868\u73b0\u5427\uff0c\u4e24\u5f20\u56fe\u7ed9\u4f60\u770b\u51fa\u6548\u679c\uff1a<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-264\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2015\u5e746\u67081\u65e5-\u4e0b\u53481.42.55.png\" alt=\"iOS Simulator Screen Shot 2015\u5e746\u67081\u65e5 \u4e0b\u53481.42.55\" width=\"250\" height=\"444\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-265\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2015\u5e746\u67081\u65e5-\u4e0b\u53481.42.59.png\" alt=\"iOS Simulator Screen Shot 2015\u5e746\u67081\u65e5 \u4e0b\u53481.42.59\" width=\"250\" height=\"444\" \/><\/p>\n<p>4\u3001\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? ViewController.m\n\/\/? Test\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 15\/6\/1.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"ViewController.h\"\n?\n@interface ViewController () &lt;UISearchDisplayDelegate, UISearchBarDelegate&gt;\n?\n@property (nonatomic, strong) NSMutableArray??? *dataArray;\n?\n@property (nonatomic, strong) NSArray?????????? *searchArray;\n?\n\/\/@property (nonatomic, strong) UISearchDisplayController *searchDisplay;\n?\n@end\n?\n@implementation ViewController\n?\n- (void)viewDidLoad\n{\n????[super viewDidLoad];\n????self.dataArray = [NSMutableArray arrayWithObjects:@\"a\", @\"b\", @\"c\", @\"d\", @\"e\", nil];\n????self.searchArray = [self.dataArray mutableCopy];\n????UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];\n????searchBar.delegate = self;\n????self.tableView.tableHeaderView = searchBar;\n\/\/??? self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];\n\/\/??? self.searchDisplay.searchResultsDataSource = self;? \/\/ \u641c\u7d22\u7ed3\u679c\u8868\u683cDataSource\n\/\/??? self.searchDisplay.searchResultsDelegate = self;??? \/\/ \u641c\u7d22\u7ed3\u679c\u8868\u683cDelegate\n}\n?\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\n{\n????return self.dataArray.count;\n}\n?\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n????UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@\"SATestCell\"];\n????if (cell == nil){\n????????cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@\"SATestCell\"];\n????}\n????cell.textLabel.text = self.dataArray[indexPath.row];\n????return cell;\n}\n?\n#pragma mark - UISearchBar\u4ee3\u7406\u65b9\u6cd5\n\/\/\/ \u641c\u7d22\u6846\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\u8c03\u7528\n- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText\n{\n????[self.dataArray removeAllObjects];\n????for (NSString *string in self.searchArray) {\n????????if ([string rangeOfString:searchText].length || searchText.length == 0){\n????????????[self.dataArray addObject:string];\n????????}\n????}\n????[self.tableView reloadData];??? \/\/ \u5982\u679c\u4f7f\u7528UISearchDisplayController\u8fd9\u53e5\u4ee3\u7801\u5219\u6ca1\u5fc5\u8981\n}\n\/\/\/ \u53d6\u6d88\u641c\u7d22\u65f6\u8c03\u7528\n- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar\n{\n????self.dataArray = [self.searchArray mutableCopy];\n????[self.tableView reloadData];\n}\n?\n@end<\/pre>\n<p>5\u3001\u518d\u6765\u770b\u770bUISearchDisplayController\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f\uff0c\u4ece\u4e0a\u56fe\u6765\u770b\uff0c\u666e\u901a\u7684UISearchBar\u7684\u641c\u7d22\u7ed3\u679c\u5728\u539f\u6709\u7684TableView\u4e0a\u5c55\u793a\uff0c\u5f53\u53d6\u6d88\u65f6\u8fd8\u539f\u539f\u6709\u7684\u6240\u6709\u6570\u636e\uff0c\u56de\u5230\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<p>6\u3001UISearchDisplayController\u5219\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e86\u4e00\u70b9\u70b9\u52a0\u5de5\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4e24\u4e2a\u65b9\u9762\uff1a\u7b2c\u4e00\u3001\u641c\u7d22\u7ed3\u679c\u4e0d\u5728\u539f\u6709\u7684\u8868\u683c\u4e0a\u5c55\u793a\uff0c\u800c\u662f\u5728\u4e00\u4e2a\u65b0\u7684TableView\u4e0a\u663e\u793a\u641c\u7d22\u7ed3\u679c\uff1b\u7b2c\u4e8c\u3001\u5c06\u641c\u7d22\u9875\u9762\u5168\u5c4f\u663e\u793a\uff0c\u8ba9\u7528\u6237\u4e13\u6ce8\u4e8e\u641c\u7d22\u8fc7\u7a0b\uff0c\u66f4\u7b80\u6d01\u7f8e\u89c2\u3002<br \/>\n<strong>\u6ce8\u610f\uff1aUISearchDisplayController\u4e0d\u80fd\u5355\u72ec\u5de5\u4f5c\uff0c\u4ed6\u9700\u8981\u57fa\u4e8e\u67d0\u4e2aUISearchBar\uff01\uff01<\/strong><\/p>\n<p>7\u3001\u5982\u679c\u4e0d\u5f88\u6e05\u695a\uff0c\u6765\uff0c\u770b\u4e0b\u6548\u679c\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-266\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2015\u5e746\u67081\u65e5-\u4e0b\u53481.43.12.png\" alt=\"iOS Simulator Screen Shot 2015\u5e746\u67081\u65e5 \u4e0b\u53481.43.12\" width=\"250\" height=\"444\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-267\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2015\u5e746\u67081\u65e5-\u4e0b\u53481.43.15.png\" alt=\"iOS Simulator Screen Shot 2015\u5e746\u67081\u65e5 \u4e0b\u53481.43.15\" width=\"250\" height=\"444\" \/><\/p>\n<p>8\u3001\u4ee3\u7801\u5b9e\u73b0\u4e0e\u4e0a\u9762\u7684\u4e00\u81f4\uff0c\u628a\u6ce8\u91ca\u6389\u7684\u90e8\u5206\u6253\u5f00\u5373\u53ef\uff0c\u4e8b\u5b9e\u4e0a\u5c31\u591a\u4e86\u90a3\u4e48\u51e0\u884c\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">@property (nonatomic, strong) UISearchDisplayController *searchDisplay;\n????self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];\n????self.searchDisplay.searchResultsDataSource = self;? \/\/ \u641c\u7d22\u7ed3\u679c\u8868\u683cDataSource\n????self.searchDisplay.searchResultsDelegate = self;??? \/\/ \u641c\u7d22\u7ed3\u679c\u8868\u683cDelegate<\/pre>\n<p>9\u3001Demo\u4e0b\u8f7d <a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/UISearchBar.zip\">UISearchBar<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4e00\u5f00\u59cb\u5462\u6211\u662f\u641e\u4e0d\u6e05\u695a\u8fd9\u4e24\u4e2a\u4e1c\u897f\u6709\u4ec0\u4e48\u5173\u7cfb\uff0c\u6216\u8005\u8bf4UISearchDisplayController\u6211\u6839\u672c\u5c31\u6ca1\u7528\u8fc7\uff0c\u90a3\u4e48\u5c31\u5148\u6765\u770b\u4e0bUISearchBar\u7684\u4f7f\u7528\u6548\u679c 2\u3001UISearchBar\u662f\u4e2a\u641c\u7d22\u5de5\u5177\u6761\uff0c\u8fd9\u4e2a\u63a7\u4ef6\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff0c\u4ed6\u4e0eUITextField\u5176\u5b9e\u6709\u70b9\u50cf\uff0c\u80fd\u591f\u76d1\u542c\u5f00\u59cb\u641c\u7d22\uff0c\u641c\u7d22\u8bcd\u53d8\u5316\uff0c\u641c\u7d22\u7ed3\u675f\u7b49\u4e00\u7cfb\u5217\u72b6\u6001\uff0c\u5e76\u8c03\u7528\u4ee3\u7406\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u529f\u80fdUITextField\u4e0d\u662f\u4e5f\u90fd\u6709\u5417\uff1f\u53ea\u662f\u4ed6\u662f\u4e13\u4e1a\u7684\u641c\u7d22\u63a7\u4ef6\uff0c\u957f\u5f97\u597d\u770b\u4e00\u70b9\u800c\u5df2\uff01 3\u3001\u6765\uff0c\u770b\u4e0bUISearchBar\u5728TableView\u4e2d\u7684\u8868\u73b0\u5427\uff0c\u4e24\u5f20\u56fe\u7ed9\u4f60\u770b\u51fa\u6548\u679c\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-project"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}