0

我可以在 UITableView 上显示音乐库。

  songsArray = [[NSMutableArray alloc]init];
MPMediaQuery *playlistQuery = [[MPMediaQuery alloc]init];
[playlistQuery setGroupingType:MPMediaGroupingTitle];
songArray = [playlistQuery items];
for (MPMediaItem *song in songArray) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];        
    [songsArray addObject:songTitle];
}
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                (NSIndexPath *)indexPath {
    cell.textLabel.text = [songsArray objectAtIndex:row];
[cell.textLabel setTextColor:[UIColor whiteColor]];

UITableView但是当我选择Cell时,我无法播放音乐。

    NSUInteger row = indexPath.row;

NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];

[appPlayer setQueueWithQuery:selectedSong];

[appPlayer play];

}
4

1 回答 1

0

啊,我看到了问题:

移动这段代码:

NSUInteger row = indexPath.row;
NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
[appPlayer setQueueWithQuery:selectedSong];
[appPlayer play];

到 UITableViewDelegate 协议方法“didSelectRowAtIndexPath”,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSString *selectedSong = [songsArray objectAtIndex:row];
    MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
    [appPlayer setQueueWithQuery:selectedSong];
    [appPlayer play];
}

编辑

文档:

于 2011-05-07T12:02:05.360 回答