将搜索栏添加到UIViewController类

时间:2015-11-02 18:49:10

标签: objective-c uitableview uisearchbar uisearchcontroller

我想在我现有的UIViewController类中添加一个搜索栏。我使用iOS 8+并且总是调试我的应用程序我将收到错误消息。

我做了:

·H

GetDynamicQuery

的.m

@interface mainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>

但是代码会给我一个警告:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResultArray count];
    }
    else{
       return [self.fullArray count];
    }
}

-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchResultArray];
    searchResultArray = [self.fullArray filteredArrayUsingPredicate:predicate];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResultArray objectAtIndex:indexPath.row];
    }
    else{
        cell.textLabel.text = [self.fullArray objectAtIndex:indexPath.row];
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:1)

不推荐使用

UISearchDisplayController,建议改为使用UISearchController

有关如何在您的应用中使用UISearchController的示例,请查看UIKit Catalog