简体   繁体   English

如何为每个单元格添加一个按钮?

[英]How do I add a button to each cell?

I currently have a program which will add the title/artist/album to each cell in a uitableview, but how do I add a button? 我目前有一个程序,可以将标题/艺术家/专辑添加到uitableview的每个单元格中,但是如何添加按钮? Every time a song changes the new title/artist/album will be put in a new cell. 每次歌曲更改时,新标题/艺术家/专辑将被放置在新单元格中。 Every time and new tag is put into a new cell I need a button to be added too. 每次将新标签放入新单元格时,我也需要添加一个按钮。 How would I do that? 我该怎么做? Down below is my code with what I currently have to add a button, but nothing works. 下面是我的代码以及当前我必须添加的按钮,但是没有任何效果。

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
}    

The UITableViewCell does not provide UIButton. UITableViewCell不提供UIButton。 UITableViewCell has several styles. UITableViewCell有几种样式。 But None of the style provides UIButton. 但是没有一种样式提供UIButton。 Here is the Documentation 这是文档

Try to write a new class MyUITableViewCell inherited from UITableViewCell。You can add an UIButton in the [MyUITableViewCell initWithStyle:reuseIdentifier:]. 尝试编写一个继承自UITableViewCell的新类MyUITableViewCell。您可以在[MyUITableViewCell initWithStyle:reuseIdentifier:]中添加一个UIButton。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM