[英]tableView:numberOfRowsInSection error when searching
I'm building an iOS application where I have a list of vehicle stock numbers and I need to search for specific ones. 我正在构建一个iOS应用程序,其中有车辆库存编号列表,我需要搜索特定的库存编号。
I've implemented the required methods for the UITableView, and set the delegate and source, etc- my tableview appears and works perfectly. 我已经为UITableView实现了必需的方法,并设置了委托和源,等等-我的tableview出现并可以正常工作。
The odd part is that when I type anything into the search box at the top (which is provided by a 'Search Bar and Search Display Controller' pre-made object), the application crashes: 奇怪的是,当我在顶部的搜索框中键入任何内容(由“搜索栏和搜索显示控制器”预制的对象提供)时,应用程序崩溃:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CarLocateViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x757d340' *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[CarLocateViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x757d340'
Which is odd, because I have implemented tableView:numerOfRowsInSection: 这很奇怪,因为我已经实现了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;
}
And this works perfectly for the table view on its own. 这对于单独使用表格视图非常有效。
I also have the search methods set up: 我还设置了搜索方法:
-(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;
}
Any help is appreciated! 任何帮助表示赞赏!
Changing comment to answer: 更改评论以回答:
This may be a stupid question, but is your tableview data source and delegate and the required functions inside the CarLocateViewController object? 这可能是一个愚蠢的问题,但是您的tableview数据源和委托以及CarLocateViewController对象内的必需函数是吗? The code you have provided doesn't exactly say where those methods are located. 您提供的代码并未完全说明这些方法的位置。
The class instance, which you have assign to DataSource
doesn't implement the UITableViewDataSource
functions. 您分配给DataSource
的类实例未实现UITableViewDataSource
函数。
@interface MyClassController :UIViewController <UITableViewDelegate,UITableViewDataSource>
try like below. 尝试如下。
myTableView.dataSource = self;
myTableView. delegate = self;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.