【问题标题】:How do I use the UISearchBar and UISearchDisplayController如何使用 UISearchBar 和 UISearchDisplayController
【发布时间】:2013-09-14 04:45:45
【问题描述】:

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

更新 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 在 iOS 8 中已弃用;请改用UISearchController

标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller


【解决方案1】:
  • 首先将 UISearchDisplayController 添加到您的表格视图中
  • 然后设置它的委托。
  • 实现以下方法。

Demo Project

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

【讨论】:

  • 其实我还在尝试实现这个。它在 numberOfRowsInSection 方法上给了我一个例外。
  • 是的,一旦我点击 UISearchBar,就会发送异常。
  • @icodebuster 谢谢例如,我想通知你一个错误,你应该实现searchBarTextDidEndEditing并将isSearching设置为NO。否则会抛出异常,因为向下滚动时从cellForRowAtIndexPath 中的错误数组加载数据。
  • 我没有做if(isSearching)检查,而是检查传入的表格视图是否等于UISearchDisplayController,我认为这是一个更安全的检查。 if(self.searchController.searchResultsTableView == tableView)
  • @icodebuster uisearchdisplaycontroller 在 ios9 中已弃用。请用新的搜索更新你的演示代码
【解决方案2】:

这是一个很好的教程如何做到这一点。只是写它太多了:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多