Search code examples
uitableviewxcode4mpmediaitemcollection

Place mediaItemCollection data in UITableView


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.


Solution

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