0

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!

3
  • 1
    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.
    – Putz1103
    Jun 18, 2013 at 14:27
  • When and how do you create allVehicles?
    – El Tomato
    Jun 18, 2013 at 14:31
  • @Putz1103 - That wasn't a stupid question at all; I had totally overlooked this. I had set my delegate for the UITableView in a different class, and did the same for the UISearchBar, but apparently the auto-generated list made by the UISearchBar automatically latches on to the ViewController class. I moved everything into the ViewController and now it doesn't crash- if you make an answer about this I'll accept it. I guess this was one of those cases where I was reading the message but not /really/ reading the message.
    – jssblck
    Jun 18, 2013 at 16:02

2 Answers 2

1

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.

0

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;
2
  • That's why you should always conform to the protocols, on your header, so you can get compiler warnings when you do not implement the required protocol methods! (comment was not for you @iPatel, but general, as a follow up to your answer)
    – Lefteris
    Jun 18, 2013 at 14:35
  • 1
    But he said the table loads properly the first time and shows all his data, therefore the datasource is at least set properly to start. I think he's trying to search the wrong class for tableview data.
    – Putz1103
    Jun 18, 2013 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.