"无法识别的选择器发送到实例" - 添加搜索栏时出错

时间:2014-05-08 17:49:46

标签: ios objective-c uitableview search unrecognized-selector

我刚刚在我的iPhone应用程序中添加了一个搜索栏,它应该搜索歌曲的TableViewController。但是,当我在iPhone上运行我的应用程序时,它会被黑屏冻结,我收到此错误:unrecognized selector sent to instance在此行:

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

在这个区块中:

- (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;
}

这是我的实施代码:

@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;
}

我不明白什么是错的?错误是:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MPConcreteMediaItem objectAtIndex:]: unrecognized selector sent to instance 0x178221e20'.

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

更改此代码:

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

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

到此:

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

答案 1 :(得分:1)

问题正在发生,原因如下:

songs = [songs objectAtIndex:indexPath.row];

您正在为MPMediaItem对象分配NSArray。下次当你尝试使用[songs objectAtIndex:indexPath.row];它会崩溃时。