2015-11-02 55 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的示例。