0
我目前有一個程序,將標題/藝術家/專輯添加到uitableview中的每個單元格,但是如何添加一個按鈕?每當一首歌改變時,新的標題/藝術家/專輯將被放入一個新的單元格中。每次將新標籤放入一個新的單元中時,我都需要添加一個按鈕。我會怎麼做?下面是我的代碼與我目前必須添加一個按鈕,但沒有任何作品。如何爲每個單元添加一個按鈕?
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
musicPlayer.beginGeneratingPlaybackNotifications()
playButton = UIButton()
let image = "playbutton.png"
playButton.setImage(UIImage(named:image), forState: .Normal)
playButton.addTarget(self, action: "play:", forControlEvents: UIControlEvents.TouchUpInside)
// Do any additional setup after loading the view.
}
func getNowPlayingItem() {
if let nowPlaying = musicPlayer.nowPlayingItem {
let title = nowPlaying[MPMediaItemPropertyTitle] as? String
let artist = nowPlaying[MPMediaItemPropertyArtist] as? String
let album = nowPlaying[MPMediaItemPropertyAlbumTitle] as? String
println("Song: \(title)")
println("Artist: \(artist)")
println("Album: \(album)")
println("\n")
self.add = [Play(name: "\(title!)\n\(artist!)\n\(album!)")]
self.table.rowHeight = UITableViewAutomaticDimension
self.table.estimatedRowHeight = 44.0
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.add.count
//return count of objects in array
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.table.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
var play: Play
play = add[indexPath.row]
cell.textLabel?.text = play.name
cell.textLabel?.numberOfLines = 3;
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
playButton.tag = indexPath.row
return cell
}
但是是否可以繼續向每個單元添加播放按鈕? – blee
我認爲你需要使用UITableViewCell的Sub類,你可以在其中添加一個按鈕並提供標記並在tableview中獲取所選項目。 –