我如何使用UISearchBar和UISearchDisplayController

You*_*wad 26 objective-c uitableview uisearchbar uisearchdisplaycontroller ios

我有一个应用程序,显示了相当多的数据UITableView.我已经在界面生成器中添加了UISearchBar和.但我不知道如何使用它.如果有人能为此提供快速解决方案,我将不胜感激.我只是要求它在您键入时工作,以在单元格(或从数组)中查找搜索查询的匹配项.UISearchDisplayControllerUITableViewUITableView

更新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;  
}
Run Code Online (Sandbox Code Playgroud)

ico*_*ter 84

  • 首先将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;
Run Code Online (Sandbox Code Playgroud)

在你的.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];
}
Run Code Online (Sandbox Code Playgroud)

现在实现表视图委托和数据源

- (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;

}
Run Code Online (Sandbox Code Playgroud)

搜索功能负责搜索

- (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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

搜索栏实施

- (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];
}
Run Code Online (Sandbox Code Playgroud)

  • 而不是做一个`if(isSearching)`检查,我检查传入的表视图是否等于`UISearchDisplayController`,我认为这是一个更安全的检查.`if(self.searchController.searchResultsTableView == tableView)` (7认同)
  • @icodebuster uisearchdisplaycontroller在ios9中折旧.请用新搜索更新你的演示代码 (2认同)

Grz*_*ski 6

这是一个很好的教程如何做到这一点.写这篇文章太过分了:)

http://www.appcoda.com/how-to-add-search-bar-uitableview/