【发布时间】:2016-03-09 21:45:07
【问题描述】:
我发现 MPMediaQuery 的深层嵌套结构难以导航。
我正在尝试让每个部分的专辑标题显示在索引 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”、“B”等),例如:
一个
艾比路
Achtung 婴儿
所有的年轻人
乙
宝贝,星星闪耀着光芒
C
宇宙的东西
等等……
【问题讨论】:
标签: ios swift mpmediaquery