我正在构建一个 iOS 应用程序,其中有一个车辆库存编号列表,我需要搜索特定的库存编号。
我已经实现了 UITableView 所需的方法,并设置了委托(delegate)和源等 - 我的表格 View 出现并完美运行。
奇怪的是,当我在顶部的搜索框中输入任何内容(由“搜索栏和搜索显示 Controller ”预制对象提供)时,应用程序崩溃:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CarLocateViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x757d340'
这很奇怪,因为我已经实现了 tableView:numerOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Defining rows for table view: %i", allVehicles.count);
return allVehicles.count;
}
这对于表格 View 本身来说非常有效。
我还设置了搜索方法:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredVehicles removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
filteredVehicles = [NSMutableArray arrayWithArray:[allVehicles filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
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;
}
感谢任何帮助!
最佳答案
更改评论以回答:
这可能是一个愚蠢的问题,但是您的 tableview 数据源和委托(delegate)以及 CarLocateViewController 对象内部所需的功能是吗?您提供的代码并未准确说明这些方法的位置。
关于ios - TableView :numberOfRowsInSection error when searching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171421/