簡體   English   中英

斯威夫特-UITableView

[英]Swift - UITableView

它是用於音頻播放列表的UITableView

import UIKit
import MediaPlayer

class TableView: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!

var tableData = MPMediaQuery.playlistsQuery()

override func viewDidLoad() {
    super.viewDidLoad()


    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.tableView.reloadData()

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.collections.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {

    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    let playlist: MPMediaPlaylist = tableData.collections[indexPath.row] as MPMediaPlaylist
    let playlistName = playlist.valueForProperty(MPMediaPlaylistPropertyName) as NSString
    cell.textLabel?.text = playlistName

    return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("Row \(indexPath.row) selected")
}
}

如何更改它,以在TableView中具有相冊?

我試圖更改var tableData = MPMediaQuery.playlistsQuery()

var tableData = MPMediaQuery.albumsQuery()

但它是不正確的,xcode給我一個錯誤:0x1002364b0:brk#1

你做了什么是一個好的開始,改變tableData到結果MPMediaQuery.albumsQuery而不是MPMediaQuery.playlistsQuery 但是,請查看cellForRowAtIndexPath方法,特別是以下三行,其中定義了每個單元格的內容:

let playlist: MPMediaPlaylist = tableData.collections[indexPath.row] as MPMediaPlaylist
let playlistName = playlist.valueForProperty(MPMediaPlaylistPropertyName) as NSString
cell.textLabel?.text = playlistName

此代碼預計tableData包括含有集合屬性MPMediaPlaylist對象但MPMediaQuery.albumsQuery方法返回MPMediaQuery用包含一個集合屬性對象MPMediaItem對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM