Place mediaItemCollection data in UITableView

梦想的初衷 提交于 2019-12-07 22:36:45

问题


I need some help placing the mediaItemCollection data in a UITableView.

Code:

// Responds to the user tapping Done after choosing music.
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {    
    [self dismissModalViewControllerAnimated: YES];

    [musicPlayer stop];
    [[MPMusicPlayerController iPodMusicPlayer] stop];
    [[MPMusicPlayerController iPodMusicPlayer] setQueueWithItemCollection:mediaItemCollection];
    [[MPMusicPlayerController iPodMusicPlayer] play];
}

The mediaItemCollection is what I need to place into my UITableView.


回答1:


I found the code :)

By declaring an MPMediaItemCollection and setting its content, I can read the mediaItemCollection when updating the UITableView's data.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"song_cell"];

    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:@"song_cell"];

    MPMediaItem *anItem = [[collection items] objectAtIndex:indexPath.row];

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

    [tableView reloadData];
}


来源:https://stackoverflow.com/questions/7128032/place-mediaitemcollection-data-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!