Search code examples
uitableviewios7uisearchdisplaycontroller

NSInvalidArgumentException - unrecognized selector sent to instance


I am using the following function to do a search using a SearchDisplayController

- (void)handleSearchForTerm:(NSString *)searchTerm{

[self.searchResults removeAllObjects]; // First clear the filtered array.

FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];


[db open];


FMResultSet *results = [db executeQuery: [NSString stringWithFormat:@"SELECT * FROM periods where (searchstartyear <= %@)  AND (searchendyear >= %@)", searchTerm, searchTerm]];

    NSMutableArray *array = [[NSMutableArray alloc] init ];
    [array removeAllObjects];
    while ([results next]) {
        Periods *searchPeriod = [[Periods alloc] init];
        searchPeriod.periodname = [results stringForColumn:@"PeriodName"];
        searchPeriod.startyear = [results stringForColumn:@"StartYear"];
        searchPeriod.endyear = [results stringForColumn:@"EndYear"];
        searchPeriod.thumb = [results stringForColumn:@"Thumb"];
        searchPeriod.childid = [results stringForColumn:@"ChildID"];
        [array addObject:searchPeriod];           
    }
    [self.searchResults addObject:array];


[db close];}    

Periods is an object

#import <UIKit/UIKit.h>

@interface Periods : NSObject

@property (nonatomic,strong) NSString *periodname;
@property (nonatomic,strong) NSString *startyear;
@property (nonatomic,strong) NSString *endyear;
@property (nonatomic,strong) NSString *thumb; 
@property (nonatomic,strong) NSString *childid;


@end

I get the NSInvalidArgumentException - unrecognized selector sent to instance when the following code is executed:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PeriodsCell";

    UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    if (tableView != [[self searchDisplayController] searchResultsTableView])
    {

        NSMutableArray *array = [periodsdataarray objectAtIndex:indexPath.section];
        NSMutableDictionary *dictionary = [array objectAtIndex:indexPath.row];




        UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
        childIDLabel.text = [dictionary valueForKey:@"ChildID"];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
        titleLabel.text = [dictionary valueForKey:@"PeriodName"];


        UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
        previewLabel.text = [NSString stringWithFormat:@"%@ - %@",[dictionary valueForKey:@"StartYear"],[dictionary valueForKey:@"EndYear"]];
        UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
        [imageview setImage:[UIImage imageNamed:[dictionary valueForKey:@"Thumb"]]];
    }
    else
    {
        Periods *periodcell = [self.searchResults objectAtIndex:indexPath.row];

        cell.tag = 666;
        UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
        childIDLabel.text = periodcell.childid;
        UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
        titleLabel.text = periodcell.periodname;


        UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
        previewLabel.text = [NSString stringWithFormat:@"%@ - %@",periodcell.startyear,periodcell.endyear];
        UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
        [imageview setImage:[UIImage imageNamed:periodcell.thumb]];


    }

    return cell;
}

...and more specifically when this line is executed:

childIDLabel.text = periodcell.childid;

What I am doing wrong?


Solution

  • The error I was making was at the handleSearchForTerm function.

    I was using the NSMutableArray *array to hold the values of my Periods object and then I was adding the array to my searchResults array (expecting that the searchResults was holding Periods). The solution was as simple as adding Periods directly to my searchResults array and ditching array at all...