【问题标题】:Get Album titles for indexed uitableview using MPMediaQuery in swift?快速使用 MPMediaQuery 获取索引 uitableview 的专辑标题?
【发布时间】: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


    【解决方案1】:

    我将回答我自己的问题。
    我已经在 SO 上发布了类似的问题,每个回复的人都说这无法完成,并且需要额外的数组以及自定义排序例程。这根本不是真的。
    此代码使用来自专辑查询的返回值:
    1) 建立一个 TableView 索引
    2) 按标题添加专辑(使用 Apple 的排序)到表 Sections
    3) 选中后开始播放专辑

    这是证明它的 Swift 代码:

    // 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)")
    

    我希望这对你们中的一些人有所帮助 - 如果是的话,请投票!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多