【发布时间】:2016-04-29 07:48:28
【问题描述】:
tableView 方法中应该如何使用搜索控制器而不是 searchDisplayController
我这里有一个示例代码
if (tableView == self.searchDisplayController.searchResultsTableView)
我在这行收到错误,它说 searchDisplayController 已弃用。
@implementation LocationsViewController
{
NSArray *_park;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.searchResult = [NSMutableArray arrayWithCapacity:[self->_park count]];
// Create a new JSONLoader with a local file URL
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"park" withExtension:@"json"];
// Load the data on a background queue...
// As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_park = [jsonLoader locationsFromJSONFile:url];
// Now that we have the data, reload the table data on the main UI thread
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});
}
//Just before showing the LocationDetailViewController, set the selected Location object
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
LocationDetailViewController *vc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
vc.location = [_park objectAtIndex:indexPath.row];
}
#pragma mark - Table View Controller Methods
//UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocationCell"];
Location *location = [_park objectAtIndex:indexPath.row];
cell.textLabel.text = location.name;
cell.detailTextLabel.text = location.address;
// cell.detailTextLabel.text = location.ide; cell.imageView.image = [UIImage imageNamed:@"location_image"];
///////////////////
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self->_park[indexPath.row];
}
/////////////////
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//////////////////////////////////////////////
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResult count];
}
else
{
return [self->_park count];
}
//////////////////////////////////////////////
return [_park count];
}
/////////////////////////////////////////////////
- (void)filterContentForSearchText:(NSString*)searchText scope: (NSString*)scope
{ [self.searchResult removeAllObjects]; NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
self.searchResult = [NSMutableArray arrayWithArray: [self->_park filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
@结束
【问题讨论】:
-
您使用的是什么语言?
-
你有这个答案然后帮助
-
如果我不知道它是什么框架语言,我不知道如何回答。
-
如果您尝试为您的 tableView 和 UISearchResultsController 以不同的方式处理 cellForRowAtIndexPath 或 didSelectRowAtIndexPath ,因此您上面提到的条件则 UISearchController 有一个名为 searchResultsController 的属性 :) 所以您可以比较 if (tableView == self. yourSearchController.searchResultsController) 提供了更多的代码来理解上下文,仅仅写一行代码不会有多大帮助
-
这是我的代码@sandeep
标签: ios objective-c search controller