ios - 如何向每个单元格添加一个按钮?

标签 ios swift

我目前有一个程序,可以将标题/艺术家/专辑添加到 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
}    

最佳答案

UITableViewCell不提供UIButton。 UITableViewCell 有多种样式。但没有一个样式提供UIButton。这是Documentation

尝试编写一个继承自UITableViewCell的新类MyUITableViewCell。可以在[MyUITableViewCell initWithStyle:reuseIdentifier:]中添加一个UIButton。

关于ios - 如何向每个单元格添加一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32365309/

相关文章:

ios - 从 NSObject 类派生安全类,用于在 iOS Swift 中进行 JSON 解析

ios - 更改NavigationView标题SwiftUI的颜色

ios - NSDateFormatter中的问题将NSDate转换为字符串

arrays - 具有惰性元素的数组

swift - 正则表达式:捕获组和空字段(SWIFT 5 | ICU 正则表达式引擎)

sql - 500 操作无法完成。 (PostgreSQL.DatabaseError 错误 1。)

objective-c - swift 。无法构建 Objective-C 模块

ios - 如何在 View Controller 的背景中设置自定义颜色

ios - 如何在 Swift 中将 BLOB 缓冲区转换为 Base64 字符串?

ios - 如何使 UITextView 在 Swift 中可选择