尝试运行我的代码时,出现上述警告消息。
NSDictionary *tempDictionary = nil;
if (self.tableView == self.searchDisplayController.searchResultsTableView) {
tempDictionary = [filteredCompanyArray objectAtIndex:indexPath.row];
}
else {
tempDictionary= [self.client_list objectAtIndex:indexPath.row];
}
它已被弃用,并进行了谷歌搜索,但我所看到的只是Swift中的教程。
我在这里遵循了Ray Wenderlich的教程http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view,但现在我陷入了困境。
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredCompanyArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
filteredCompanyArray = [NSMutableArray arrayWithArray:[self.client_list filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
最佳答案
下面的食谱对我有用。我刚刚成功更新了以前的iOS 7代码的多个方案。
也感谢Updating to the iOS 8 Search Controller和Apple's API Reference的启发
@interface YOURTableviewController : UIViewController <UITableViewDelegate, UITableViewDataSource, /*UISearchDisplayDelegate, <- Removed*/ UISearchResultsUpdating /* Added */, UISearchControllerDelegate /* Added */>
{
// New property
@property (nonatomic, strong) UISearchController *searchController;
// ...
@implementation YOURTableviewController
@synthesize searchController; // New property
并在didLoad方法中初始化新属性
- (void)viewDidLoad
{
// New code start -
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor orangeColor];
[self.tableview setTableHeaderView:self.searchController.searchBar];
self.definesPresentationContext = YES;
// - New code end
// Previous code
//self.searchDisplayController.searchBar.barTintColor = [UIColor orange];
[super viewDidLoad];
}
#pragma mark -
#pragma mark UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
{
NSString *searchString = _searchController.searchBar.text;
[self searchForText:searchString];
[self.tableview reloadData];
}
#pragma mark -
#pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self updateSearchResultsForSearchController:self.searchController];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
// Extra method
- (void)searchForText:(NSString *)searchString
{
/* Put here everything that is in the method:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
...BUT WITH NO RETURN VALUE */
}
#pragma mark -
#pragma mark UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController
{
//..
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
//..
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
//..
}
- (void)didDismissSearchController:(UISearchController *)searchController
{
//..
}
#pragma mark -
#pragma mark UISearchDisplayDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{ ... }
像这样的代码:
if (tableView == self.searchDisplayController.searchResultsTableView)
可以替换为
if (self.searchController.isActive)
像这样的代码:
[self.searchDisplayController setActive:YES];
可以替换为
[self.searchController setActive:YES];