2015-11-02 75 views
-3

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

我所做的:

.H

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

.M

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

但代码会给我一个警告:

searchDisplayController is deprecated: first deprecated in iOS 8.0 

我该如何解决这个问题?

+0

查看“UISearchDisplayController”的文档。它告诉你现在要做什么。 – rmaddy

+0

我发现这个:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchDisplayController_Class/,但它不是真的有用... – hantoren

+1

它怎么没有帮助?您是否至少阅读过这些文档的第一段? – rmaddy

回答

1

UISearchDisplayController已弃用,建议您使用UISearchController来代替。

查看UIKit Catalog关于如何在您的应用程序中使用UISearchController的示例。