Question

I just added a search bar to my iPhone app which should search through a TableViewController of songs. However, when I run my app on my iPhone, it freezes with a black screen and I get this error: unrecognized selector sent to instance on this line:

MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

in this block:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        songs = [searchResults objectAtIndex:indexPath.row];
    } else {
        songs = [songs objectAtIndex:indexPath.row];
    }

    MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

    cell.textLabel.text = [rowItem valueForProperty:MPMediaItemPropertyTitle];
    cell.detailTextLabel.text = [rowItem valueForProperty:MPMediaItemPropertyArtist];

    return cell;
}

This is my implementation code:

@implementation SongsViewController
{
    NSArray *searchResults;
    NSArray *songs;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
    songs = [songsQuery items];  
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    } else {
        return [songs count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        songs = [searchResults objectAtIndex:indexPath.row];
    } else {
        songs = [songs objectAtIndex:indexPath.row];
    }

    MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

    cell.textLabel.text = [rowItem valueForProperty:MPMediaItemPropertyTitle];
    cell.detailTextLabel.text = [rowItem valueForProperty:MPMediaItemPropertyArtist];

    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [songs filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

I do not understand what's wrong? The error is: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MPConcreteMediaItem objectAtIndex:]: unrecognized selector sent to instance 0x178221e20'.

Any help would be much appreciated!

Was it helpful?

Solution

Change this code:

if (tableView == self.searchDisplayController.searchResultsTableView) {
    songs = [searchResults objectAtIndex:indexPath.row];
} else {
    songs = [songs objectAtIndex:indexPath.row];
}

MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

to this:

MPMediaItem *rowItem;
if (tableView == self.searchDisplayController.searchResultsTableView) {
    rowItem = [searchResults objectAtIndex:indexPath.row];
} else {
    rowItem = [songs objectAtIndex:indexPath.row];
}

OTHER TIPS

Issue is happening because of the following line:

songs = [songs objectAtIndex:indexPath.row];

You are assigning MPMediaItem to the NSArray object. And next time when you try to use [songs objectAtIndex:indexPath.row]; It'll crash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top