Search code examples
iosobjective-cuitableviewmpmediaitem

Array count for numberOfRowsInSection


I'm grabbing music albums from the phone's music library. I want to put the albums in sections (with the album cover as the section header image) and then the albums songs will fill in each sections table.

I know how to get an array of albums, which will be used for numberOfSectionsInTableView.

But I'm not sure how the array of album tracks works for numberOfRowsInSection. Because I essentially need to navigate into the array of albums and then navigate inside of each album, which is two levels deep.

Can anyone help me with this?

**AlbumsTableViewController.h**

- (void)viewDidLoad {
    [super viewDidLoad];

    Albums *albumsFromClass = [[Albums alloc] init];
    albumsArrayForTVC = [albumsFromClass getAlbumsArray];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return albumsArrayForTVC.count;
}

**Albums.h**

- (NSArray *)getAlbumsArray {
    _albumsMPMediaQuery = [MPMediaQuery albumsQuery];
    _albumsArray = [_albumsMPMediaQuery collections];
    return _albumsArray;
}

Solution

  • Print the array of albums, or the first object in the array, to see how the data is structured. Assuming it is nested arrays, albumsArrayForTVC[0] should grab an array of songs for a specific album. You can save this as a variable or do something like albumsArrayForTVC[0][2] to grab the third song in the first album.