简体   繁体   中英

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.

I've implemented the required methods for the UITableView, and set the delegate and source, etc- my tableview appears and works perfectly.

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'

Which is odd, because I have implemented 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? 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.

 @interface MyClassController :UIViewController <UITableViewDelegate,UITableViewDataSource>

try like below.

myTableView.dataSource = self;
myTableView. delegate = self;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM