在 UITableView 中实现 UISearchController Objective-C
Implement UISearchController in UITableView Objective-C
我正在按照本教程在我的项目中实施 UISearchController
:http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html
我成功找到了已弃用的方法 (searchDisplayController
),但我想实现 UISearchController
。
好吧,在我的 viewDidLoad 中我实现了 :
//UISearchController
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchBar.scopeButtonTitles = @[];
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
在我的 numberOfRowsInSection
中,我让 searchDisplayController
的旧代码为:
if (table == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else
{
return [self.allUsers count];
}
在我的 cellForRowAtIndexPath
中:
NSArray *sourceData = (tableView == self.searchDisplayController.searchResultsTableView ? self.searchResults : self.allUsers);
PFUser *selected = [sourceData objectAtIndex:indexPath.row];
NSString *name = [[sourceData objectAtIndex:indexPath.row] valueForKey:@"surname"];
cell.textLabel.text = name;
并添加了这些方法:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[self.tableView reloadData];
NSLog(@"updateSearchResultsForSearchController");
}
- (void)searchForText:(NSString*)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"surname contains[c] %@", searchText];
self.searchResults = [self.allUsers filteredArrayUsingPredicate:predicate];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
嗯,当我点击搜索栏并点击一些词时,日志 updateSearchResultsForSearchController
出现了。但是我搜索的人没有出现。我不知道我必须为那个作品实施什么?也许我的 searchForText:searchString
方法不对?
或者我可能需要在 cellForRowAtIndexPath
和 numberOfRowsInSection
中添加一些内容?
首先,您应该检查 self.searchResults.count
。如果self.searchResults.count > 0
,方法searchForText:searchString
是对的。所以你在tableView中检查问题。
我正在按照本教程在我的项目中实施 UISearchController
:http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html
我成功找到了已弃用的方法 (searchDisplayController
),但我想实现 UISearchController
。
好吧,在我的 viewDidLoad 中我实现了 :
//UISearchController
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchBar.scopeButtonTitles = @[];
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
在我的 numberOfRowsInSection
中,我让 searchDisplayController
的旧代码为:
if (table == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else
{
return [self.allUsers count];
}
在我的 cellForRowAtIndexPath
中:
NSArray *sourceData = (tableView == self.searchDisplayController.searchResultsTableView ? self.searchResults : self.allUsers);
PFUser *selected = [sourceData objectAtIndex:indexPath.row];
NSString *name = [[sourceData objectAtIndex:indexPath.row] valueForKey:@"surname"];
cell.textLabel.text = name;
并添加了这些方法:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[self.tableView reloadData];
NSLog(@"updateSearchResultsForSearchController");
}
- (void)searchForText:(NSString*)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"surname contains[c] %@", searchText];
self.searchResults = [self.allUsers filteredArrayUsingPredicate:predicate];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
嗯,当我点击搜索栏并点击一些词时,日志 updateSearchResultsForSearchController
出现了。但是我搜索的人没有出现。我不知道我必须为那个作品实施什么?也许我的 searchForText:searchString
方法不对?
或者我可能需要在 cellForRowAtIndexPath
和 numberOfRowsInSection
中添加一些内容?
首先,您应该检查 self.searchResults.count
。如果self.searchResults.count > 0
,方法searchForText:searchString
是对的。所以你在tableView中检查问题。