我如何使用UISearchBar和UISearchDisplayController
我有一个应用程序在UITableView
显示相当多的数据。 我已经在Interface Builder中添加了UISearchBar
和UISearchDisplayController
到UITableView
。 但我不知道如何使用它。 如果有人能提供一个快速的解决scheme,我将不胜感激。 我只是要求它工作,你键入查找在UITableView
单元格(或从一个数组)的search查询匹配。
更新1 :这是来自numberOfRowsInSection
方法的代码:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (isSearching) { return [searchResults count]; } else { if (section == 0) { return 1; } else if (section == 1) { return [basicQuantities count]; } else if (section == 2) { return [physicalQuantities count]; } } return nil; }
- 首先添加UISearchDisplayController到你的表格视图
- 然后设置它的代表。
- 实施以下方法。
演示项目
在你的.h文件中
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { NSMutableArray *contentList; NSMutableArray *filteredContentList; BOOL isSearching; } @property (strong, nonatomic) IBOutlet UITableView *tblContentList; @property (strong, nonatomic) IBOutlet UISearchBar *searchBar; @property (strong, nonatomic) IBOutlet UISearchDisplayController *searchBarController;
在你的.m文件中
填写样本数据(仅用于演示目的)
- (void)viewDidLoad { [super viewDidLoad]; contentList = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPod touch", @"iMac", @"Mac Pro", @"iBook",@"MacBook", @"MacBook Pro", @"PowerBook", nil]; filteredContentList = [[NSMutableArray alloc] init]; }
现在实现表视图委托和数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (isSearching) { return [filteredContentList count]; } else { return [contentList count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... if (isSearching) { cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row]; } else { cell.textLabel.text = [contentList objectAtIndex:indexPath.row]; } return cell; }
searchfunction负责search
- (void)searchTableList { NSString *searchString = searchBar.text; for (NSString *tempStr in contentList) { NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; if (result == NSOrderedSame) { [filteredContentList addObject:tempStr]; } } }
search栏实施
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { isSearching = YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSLog(@"Text change - %d",isSearching); //Remove all objects first. [filteredContentList removeAllObjects]; if([searchText length] != 0) { isSearching = YES; [self searchTableList]; } else { isSearching = NO; } // [self.tblContentList reloadData]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"Cancel clicked"); } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"Search Clicked"); [self searchTableList]; }
这是一个很好的教程如何做到这一点。 这只是写太多了:)