简体   繁体   English

搜索时发生tableView:numberOfRowsInSection错误

[英]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.

相关问题 在tableView(_:numberOfRowsInSection :)中引发错误时该怎么办? - What to do when error thrown in tableView(_:numberOfRowsInSection:)? 什么时候是tableView:numberOfRowsInSection:在UITableView中调用? - When is tableView:numberOfRowsInSection: called in UITableView? 重新加载 tableView 时将 numberOfRowsInSection 发送到 UIProxyObject - numberOfRowsInSection being sent to UIProxyObject when reloading tableView 获取错误不明确地使用tableView(_:numberOfRowsInSection :) - Getting error Ambiguous use of tableView(_:numberOfRowsInSection:) 对成员'tableView(_:numberOfRowsInSection :)'的模糊引用为什么会出现此错误? - Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' Why this error? FirstViewController tableView:numberOfRowsInSection:]静态单元格错误 - FirstViewController tableView:numberOfRowsInSection:] error with Static Cells 使用UITableView时,ios-tableView:numberOfRowsInSection…NSInvalidArgumentException - ios - tableView:numberOfRowsInSection …NSInvalidArgumentException when working with UITableView Swift tableview numberOfRowsInSection返回nil致命错误可选值 - Swift tableview numberOfRowsInSection return nil fatal error Optional value SWIFT:为tableView计算numberOfRowsInSection时,数组索引超出范围 - SWIFT: Array index out of range' when calculating numberOfRowsInSection for tableView 使用tableView numberOfRowsInSection时遇到问题 - Trouble using tableView numberOfRowsInSection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM