2
我發現MPMediaQuery的深層嵌套結構很難導航。在swift中使用MPMediaQuery獲取索引uitableview的唱片集標題?
我試圖讓每個部分的專輯標題顯示在索引UITableView中。
基本的查詢和代碼來獲取所有專輯:
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))")
}
該處理的TableView索引的東西:
// 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)!
}
我不知所措,以確定如何打印出專輯標題對於每個部分(其中a部分是 「A」, 「B」,等等),如:
甲
Abbey Road的
注意點兒!寶貝
所有年輕花花公子
乙
嬰兒星光閃耀明亮
Ç
宇宙的事情
等.....