[英]What to do when error thrown in tableView(_:numberOfRowsInSection:)?
[英]tableView:numberOfRowsInSection error when searching
我正在构建一个iOS应用程序,其中有车辆库存编号列表,我需要搜索特定的库存编号。
我已经为UITableView实现了必需的方法,并设置了委托和源,等等-我的tableview出现并可以正常工作。
奇怪的是,当我在顶部的搜索框中键入任何内容(由“搜索栏和搜索显示控制器”预制的对象提供)时,应用程序崩溃:
*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[CarLocateViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例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;
}
这对于单独使用表格视图非常有效。
我还设置了搜索方法:
-(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数据源和委托以及CarLocateViewController对象内的必需函数是吗? 您提供的代码并未完全说明这些方法的位置。
您分配给DataSource
的类实例未实现UITableViewDataSource
函数。
@interface MyClassController :UIViewController <UITableViewDelegate,UITableViewDataSource>
尝试如下。
myTableView.dataSource = self;
myTableView. delegate = self;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.