我正在尝试创建一个简单的音乐播放器,它在 UITableView
中列出我设备上的所有歌曲,并将其分成字母部分(例如 Apple Music Player)。
有两个地方我想不通:
- 获取每个部分的歌曲数量,以便我可以创建 我表格中每个部分的正确行数
- 访问每个部分中的项目以填充部分单元格
我的代码已经很长了,所以我会把相关信息放在这里:
我得到这样的歌曲列表:
let songsQry:MPMediaQuery = MPMediaQuery.songsQuery()
这是我不确定的地方:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//How can I get the number of songs per Section to return??
// this does not work:
return songsQry.itemSections![0].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//How do I get the song for the Section and Row??
cell.textLabel?.text = ????
return cell
}
最佳答案
我要回答我自己的问题。
我已经在 SO 上发布了类似的问题,每个回复的人都说这无法完成,需要额外的数组以及自定义排序例程。那根本不是真的。
但是请注意,在我的示例代码中,我展示了如何为相册查询获得相同类型的结果。艺术家查询的代码几乎相同。
此代码使用专辑查询的返回值:
1)建立TableView索引
2) 按标题添加专辑(使用 Apple 的排序)到表格部分
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)")
我希望这对你们中的一些人有所帮助 - 如果是的话请投票!
关于ios - 如何使用 Swift 将带有部分的歌曲列表添加到 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35819616/