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

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

问题描述

我有一个应用程序,它在 UITableView 中显示了大量数据。我已经在Interface Builder中将 UISearchBar UISearchDisplayController 添加到 UITableView 。但我不知道如何使用它。如果有人能为此提供快速解决方案,我将不胜感激。我只是要求它在您输入时工作,以便在 UITableView 单元格(或从数组中)中找到搜索查询的匹配项。

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 :以下是 numberOfRowsInSection 方法的代码:

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


推荐答案


  • 首先将 UISearchDisplayController 添加到表视图中

  • 然后设置其委托。

  • 实现以下方法。

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

      在.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;
      
      }
      

      搜索功能负责搜索

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

      搜索栏实施

      - (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天全站免登陆