I'm finding the deeply nested structure of the MPMediaQuery difficult to navigate.
I'm trying to get the title(s) of albums for each Section to display in an indexed UITableView.
The basic query and code to get all albums:
let myQuery:MPMediaQuery = MPMediaQuery.albumsQuery()
myQuery.groupingType = MPMediaGrouping.Album
let allAlbums = myQuery.collections
// This prints the total number of albums (either way works)
// Or so I thought - but this does not give the album count per section
// I don't know what this is returning!
print("number albums \(myQuery.collections?.count)")
print("number albums \(allAlbums?.count)")
// this prints out the title of each album
for album in allAlbums!{
print("---------------")
print("albumTitle \(album.representativeItem?.albumTitle)")
print("albumTitle \(album.representativeItem?.valueForProperty(MPMediaItemPropertyAlbumTitle))")
}
This handles the TableView Index stuff:
// create index title
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
let sectionIndexTitles = myQuery.itemSections!.map { $0.title }
return sectionIndexTitles
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return index
}
// tableview
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return (myQuery.itemSections![section].title)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// print("number of sections \(myQuery.itemSections?.count)")
return (myQuery.itemSections?.count)!
}
I'm at a loss to determine how to print out the Album titles for each section (where a section is "A", "B", etc) like:
A
Abbey Road
Achtung Baby
All The Young Dudes
B
Baby The Stars Shine Bright
C
Cosmic Thing
etc.....