ios - 我如何使用 UISearchBar 和 UISearchDisplayController

标签 ios objective-c uitableview uisearchbar uisearchdisplaycontroller

我有一个应用程序可以在 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 添加到您的 TableView
  • 然后设置它的委托(delegate)。
  • 实现以下方法。

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

现在实现 TableView 委托(delegate)和数据源

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

关于ios - 我如何使用 UISearchBar 和 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18719517/

相关文章:

ios - SWIFT - 在 vi​​ewdidload 之外看不到属性(property)

ios - UITextField 中的文本向下移动并闪烁?

ios - Mulipartpost NSURLConnection over 3G 网络问题

c# - Xamarin.iOS 上的 sqlite 中的缓慢 SELECT

ios - UITableView 空白单元格

ios - 奇怪的 UICollectionViewCell 行为如果 float 大小

iphone - Adhoc App无法安装在设备上

ios - NSUserDefaults 为 valueForKey 返回 null

iphone - Objective-c iPhone 中的文字转语音

swift - UITableView内容删除后没有被删除? [Swift 3.0 - Xcode 8]