我目前有一个程序会在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
}
答案 0 :(得分:0)
UITableViewCell不提供UIButton。 UITableViewCell有几种样式。但没有一种风格提供UIButton。这是Documentation
尝试编写一个继承自UITableViewCell的新类MyUITableViewCell。您可以在[MyUITableViewCell initWithStyle:reuseIdentifier:]中添加一个UIButton。