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

用户名

我刚刚在我的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'.

任何帮助将非常感激!

rmaddy

更改此代码:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CoreData设置关系给出错误'-[WeatherObservation count]:无法识别的选择器已发送到实例

来自分类Dev

调用addTarget时,UIView类中的“无法识别的选择器已发送到实例”

来自分类Dev

存档数据时将无法识别的选择器发送到实例(NSCoding)

来自分类Dev

无法识别的选择器在分配数组时发送到实例

来自分类Dev

错误:点击UISegmentedControl时,无法识别的选择器发送到实例

来自分类Dev

无法识别的选择器发送到实例+按下UIBarButtonItem时崩溃

来自分类Dev

使用UITableViewCell时将无法识别的选择器发送到实例

来自分类Dev

每当输入文本时,应用崩溃,无法识别的选择器发送到实例

来自分类Dev

无法为UIButton添加目标-尽管方法在同一类中,但无法识别的选择器已发送到实例

来自分类Dev

无法识别的选择器通过创建工具栏按钮发送到实例

来自分类Dev

更新核心数据模型后出错-无法识别的选择器发送到实例

来自分类Dev

'NSInvalidArgumentException',原因:'-[UIView setPDFPage:]:集成ZoomingPDFViewer时将无法识别的选择器发送到实例

来自分类Dev

NSPredicate:当使用filteredArrayUsingPredicate过滤NSDateComponents数组时,无法识别的选择器发送到实例

来自分类Dev

出现UIActivityViewController时,iOS 9崩溃:[UIView _fromWindowOrientation]无法识别的选择器发送到实例(UIView)

来自分类Dev

从另一个类触摸时的 Swift 按钮操作选择器:无法识别的选择器发送到实例

来自分类Dev

标签栏>拆分> NavigationController结果显示在[MasterViewController topViewController]中:无法识别的选择器发送到实例:核心数据

来自分类Dev

自定义AppDelegate,添加ViewController属性,然后设置此属性:无法识别的选择器发送到实例0x16574320

来自分类Dev

使用AppDelegate tableView:numberOfRowsInSection启动时,应用程序崩溃:无法识别的选择器发送到实例

来自分类Dev

我认为我在分配segmentcontrol时犯了一些错误。我遇到了一个错误[无法识别的选择器发送到实例]

来自分类Dev

尝试将 NSDictionary 从 swift 类传递到目标 c 类时,无法识别的选择器发送到实例

来自分类Dev

尝试在Swift中触发NSTimer对象时,WatchKit“无法识别的选择器已发送至实例”

来自分类Dev

Swift选择器-无法识别的选择器已发送到实例

来自分类Dev

无法识别的选择器已发送到FBSDKProfile类别的实例

来自分类Dev

iOS 6.1 / 7-Cordova 3.1.0 + AngularJS-单击/滚动到具有[NSCFSet opacity]的HTML / CSS视图时,应用程序崩溃:无法识别的选择器发送到实例

来自分类Dev

无法解决“无法识别的选择器已发送到实例”错误

来自分类Dev

无法解决错误“ [__NSCFBoolean长度]:无法识别的选择器已发送到实例”

来自分类Dev

-[UIView setAdUnitID1:]:无法识别的选择器已发送到实例

来自分类Dev

iCarousel-numberOfItemsInCarousel:无法识别的选择器已发送到实例

来自分类Dev

无法识别的选择器已发送到实例。NSInvalidArgumentExpection

Related 相关文章

  1. 1

    CoreData设置关系给出错误'-[WeatherObservation count]:无法识别的选择器已发送到实例

  2. 2

    调用addTarget时,UIView类中的“无法识别的选择器已发送到实例”

  3. 3

    存档数据时将无法识别的选择器发送到实例(NSCoding)

  4. 4

    无法识别的选择器在分配数组时发送到实例

  5. 5

    错误:点击UISegmentedControl时,无法识别的选择器发送到实例

  6. 6

    无法识别的选择器发送到实例+按下UIBarButtonItem时崩溃

  7. 7

    使用UITableViewCell时将无法识别的选择器发送到实例

  8. 8

    每当输入文本时,应用崩溃,无法识别的选择器发送到实例

  9. 9

    无法为UIButton添加目标-尽管方法在同一类中,但无法识别的选择器已发送到实例

  10. 10

    无法识别的选择器通过创建工具栏按钮发送到实例

  11. 11

    更新核心数据模型后出错-无法识别的选择器发送到实例

  12. 12

    'NSInvalidArgumentException',原因:'-[UIView setPDFPage:]:集成ZoomingPDFViewer时将无法识别的选择器发送到实例

  13. 13

    NSPredicate:当使用filteredArrayUsingPredicate过滤NSDateComponents数组时,无法识别的选择器发送到实例

  14. 14

    出现UIActivityViewController时,iOS 9崩溃:[UIView _fromWindowOrientation]无法识别的选择器发送到实例(UIView)

  15. 15

    从另一个类触摸时的 Swift 按钮操作选择器:无法识别的选择器发送到实例

  16. 16

    标签栏>拆分> NavigationController结果显示在[MasterViewController topViewController]中:无法识别的选择器发送到实例:核心数据

  17. 17

    自定义AppDelegate,添加ViewController属性,然后设置此属性:无法识别的选择器发送到实例0x16574320

  18. 18

    使用AppDelegate tableView:numberOfRowsInSection启动时,应用程序崩溃:无法识别的选择器发送到实例

  19. 19

    我认为我在分配segmentcontrol时犯了一些错误。我遇到了一个错误[无法识别的选择器发送到实例]

  20. 20

    尝试将 NSDictionary 从 swift 类传递到目标 c 类时,无法识别的选择器发送到实例

  21. 21

    尝试在Swift中触发NSTimer对象时,WatchKit“无法识别的选择器已发送至实例”

  22. 22

    Swift选择器-无法识别的选择器已发送到实例

  23. 23

    无法识别的选择器已发送到FBSDKProfile类别的实例

  24. 24

    iOS 6.1 / 7-Cordova 3.1.0 + AngularJS-单击/滚动到具有[NSCFSet opacity]的HTML / CSS视图时,应用程序崩溃:无法识别的选择器发送到实例

  25. 25

    无法解决“无法识别的选择器已发送到实例”错误

  26. 26

    无法解决错误“ [__NSCFBoolean长度]:无法识别的选择器已发送到实例”

  27. 27

    -[UIView setAdUnitID1:]:无法识别的选择器已发送到实例

  28. 28

    iCarousel-numberOfItemsInCarousel:无法识别的选择器已发送到实例

  29. 29

    无法识别的选择器已发送到实例。NSInvalidArgumentExpection

热门标签

归档