我如何使用 UISearchBar 和 UISearchDisplayController [英] How do I use the UISearchBar and UISearchDisplayController

查看:19
本文介绍了我如何使用 UISearchBar 和 UISearchDisplayController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它在 UITableView 中显示了相当多的数据.我已经将 Interface Builder 中的 UISearchBarUISearchDisplayController 添加到 UITableView 中.但我不知道如何使用它.如果有人可以为此提供快速解决方案,我将不胜感激.我只需要它在您键入时在 UITableView 单元格(或从数组中)中查找搜索查询的匹配项.

UPDATE 1:这是numberOfRowsInSection 方法中的代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{如果(正在搜索){返回 [搜索结果计数];}别的 {如果(部分== 0){返回 1;}否则如果(部分== 1){返回 [basicQuantities count];}否则如果(部分== 2){返回[物理数量计数];}}返回零;}

解决方案

  • 首先将 UISearchDisplayController 添加到您的表格视图中
  • 然后设置它的委托.
  • 实现以下方法.

演示项目

在您的 .h 文件中

 @interface ViewController : UIViewController {NSMutableArray *contentList;NSMutableArray *filteredContentList;BOOL 正在搜索中;}@property (strong, nonatomic) IBOutlet UITableView *tblContentList;@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchBarController;

在您的 .m 文件中

填充示例数据(可选仅用于演示目的)

- (void)viewDidLoad {[超级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 {//返回段数.返回 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {//返回部分中的行数.如果(正在搜索){返回 [filteredContentList 计数];}别的 {返回 [内容列表计数];}}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {静态 NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(单元格 == 零){单元格 = [[UITableViewCell 分配] initWithStyle:UITableViewCellStyleDefault 重用标识符:CellIdentifier];}//配置单元格...如果(正在搜索){cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];}别的 {cell.textLabel.text = [contentList objectAtIndex:indexPath.row];}返回单元格;}

负责搜索的搜索功能

- (void)searchTableList {NSString *searchString = searchBar.text;for (NSString *tempStr in contentList) {NSComparisonResult 结果 = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];如果(结果 == NSOrderedSame){[filteredContentList addObject:tempStr];}}}

搜索栏实现

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {正在搜索 = 是;}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {NSLog(@"文本更改 - %d",isSearching);//首先删除所有对象.[filteredContentList removeAllObjects];if([搜索文本长度] != 0) {正在搜索 = 是;[自查表列表];}别的 {正在搜索 = 否;}//[self.tblContentList reloadData];}- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {NSLog(@"取消点击");}- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {NSLog(@"点击搜索");[自查表列表];}

I have an app which displays quite a lot of data in a UITableView. I already added the UISearchBar and UISearchDisplayController in Interface Builder to the UITableView. But I do not know how to use it. If someone could provide a quick solution to this, I would be grateful. I just require it to work as you type to find matches of the search query in the UITableView cells (or from an array).

UPDATE 1: Here's the code from thenumberOfRowsInSection method:

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

解决方案

  • First add the UISearchDisplayController to your table view
  • Then set its delegate.
  • Implement the following methods.

Demo Project

In your .h File

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

In your .m File

Filling the sample data (Optional Only For Demo Purpose)

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

Now implement the Table View Delegate and Datasource

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

}

Search Function Responsible For Searching

- (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 Bar Implementation

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

这篇关于我如何使用 UISearchBar 和 UISearchDisplayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆