2016-03-09 12 views
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的
注意點兒!寶貝
所有年輕花花公子

嬰兒星光閃耀明亮
Ç
宇宙的事情

等.....

回答

3

我要回答我的問題。
我已經在SO上發佈了類似的問題,所有人都回復說這不能完成,需要額外的數組以及自定義排序例程。這根本不是事實。
此代碼使用從相冊中查詢到返回:
1)建立一個TableView中指數
2)按標題)添加相冊(使用蘋果的分類表第
3)選擇時開始播放專輯

這裏的銀行代碼來證明這一點:

// Set up a basic Albums query 
let qryAlbums = MPMediaQuery.albumsQuery() 
qryAlbums.groupingType = MPMediaGrouping.Album 

// This chunk handles the TableView Index 
    //create index title 
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { 
     let sectionIndexTitles = qryAlbums.itemSections!.map { $0.title } 
     return sectionIndexTitles 
    } 

    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
     return index 
    } 

// This chunk sets up the table Sections and Headers 
    //tableview 
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return (qryAlbums.itemSections![section].title) 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return (qryAlbums.itemSections?.count)! 
    } 

// Get the number of rows per Section - YES SECTIONS EXIST WITHIN QUERIES 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {   
     return qryAlbums.collectionSections![section].range.length 
    } 

// Set the cell in the table 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 

    // i'm only posting the pertinent Album code here. 
    // you'll need to set the cell details yourself. 

     let currLoc = qryAlbums.collectionSections![indexPath.section].range.location 
     let rowItem = qryAlbums.collections![indexPath.row + currLoc] 
     //Main text is Album name 
     cell.textLabel!.text = rowItem.items[0].albumTitle 
     // Detail text is Album artist 
     cell.detailTextLabel!.text = rowItem.items[0].albumArtist! 
     // Or number of songs from the current album if you prefer 
     //cell.detailTextLabel!.text = String(rowItem.items.count) + " songs" 

     // Add the album artwork 
     var artWork = rowItem.representativeItem?.artwork   
     let tableImageSize = CGSize(width: 10, height: 10) //doesn't matter - gets resized below 
     let cellImg: UIImageView = UIImageView(frame: CGRectMake(0, 5, myRowHeight-10, myRowHeight-10)) 
     cellImg.image = artWork?.imageWithSize(tableImageSize) 
     cell.addSubview(cellImg) 

     return cell 
    } 

// When a user selects a table row, start playing the album 
// This assumes the myMP has been properly declared as a MediaPlayer 
// elsewhere in code !!! 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let currLoc = qryAlbums.collectionSections![indexPath.section].range.location 
     myMP.setQueueWithItemCollection(qryAlbums.collections![indexPath.row + currLoc]) 
     myMP.play() 
    } 

而且,這裏有一些有用的注意事項:

1)列出所有的專輯從查詢:

for album in allCollections!{ 
    print("---------------") 
    print("albumTitle \(album.items[0].albumTitle)") 
    print("albumTitle \(album.representativeItem?.albumTitle)") 
    print("albumTitle \(album.representativeItem?.valueForProperty(MPMediaItemPropertyAlbumTitle))") 
} // each print statement is another way to get the title 

2)打印出來的部分查詢,看看它是如何構建的:

print("xxxx \(qryAlbums.collectionSections)") 

我希望這有助於一些你 - 如果是這樣起來的一票!

相關問題