Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios &引用;已将无法识别的选择器发送到实例";-添加搜索栏时出错_Ios_Objective C_Uitableview_Search_Unrecognized Selector - Fatal编程技术网

Ios &引用;已将无法识别的选择器发送到实例";-添加搜索栏时出错

Ios &引用;已将无法识别的选择器发送到实例";-添加搜索栏时出错,ios,objective-c,uitableview,search,unrecognized-selector,Ios,Objective C,Uitableview,Search,Unrecognized Selector,我刚刚在我的iPhone应用程序中添加了一个搜索栏,可以通过TableViewController搜索歌曲。但是,当我在iPhone上运行我的应用程序时,它会冻结在一个黑屏上,我会收到以下错误:无法识别的选择器发送到这一行的实例: MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row]; 在该区块中: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRow

我刚刚在我的iPhone应用程序中添加了一个搜索栏,可以通过TableViewController搜索歌曲。但是,当我在iPhone上运行我的应用程序时,它会冻结在一个黑屏上,我会收到以下错误:
无法识别的选择器发送到这一行的实例

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;
}
我不明白怎么了?错误为:
***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[MPConcreteMediaItem objectAtIndex:]:无法识别的选择器已发送到实例0x178221e20'。


任何帮助都将不胜感激

问题的发生是由于以下原因:

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

更改此代码:

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

你有没有用谷歌搜索过这条消息?投票重新打开,因为这个“重复”问题的答案中没有任何东西可以帮助解决这个问题。谢谢你帮助我,我该如何解决这个问题?我应该用什么替换“songs=[songs objectAtIndex:indexPath.row];”具有Thanks@user3127576:删除else部分并选中谢谢,它现在加载并显示歌曲,但当我在搜索栏中点击键盘上的某个字符时,我得到一个SIGABRT错误-“***由于未捕获的异常“NSUnknownKeyException”终止应用程序,原因:”[valueForUndefinedKey:]:该类与键值编码不兼容。”:SThanks@user3127576:这些崩溃来自您的搜索显示控制器代理。检查NSpredicate我不确定出了什么问题,那里一切都很好到底是哪一行代码导致了这个错误?很可能这个新错误(与您的原始问题无关)来自您的搜索谓词。结束这个问题,因为我们解决了这个问题中的错误。如果需要,发布关于新错误的新问题。但首先,您需要设置Xcode以便更好地调试,这样您就可以先进行一些自己的调试。请看@downvoter请解释,这样我们大家都可以学习。@HotLicks所以你对我的好答案投了否决票,因为你认为这个问题不好?那太可悲了。这个问题没有什么不好的。我投票结束了许多糟糕的问题。这不是一个问题,我当然不值得投反对票,因为有人不喜欢这个问题。@HotLicks我不想在这个问题上引起大的争论,但仅仅因为这个问题是关于“未识别的选择器”错误,并不意味着它会自动重复同一错误上的其他一般问题。这个问题显示了代码,它显示了全部错误。这比80%的糟糕问题要好。即使OP阅读了一些其他相关问题,也不意味着OP会理解如何修复代码来解决错误。这不是一个非常糟糕的问题。不要惩罚试图帮助别人的人。需要考虑的事情。