Ios 如何为每首歌曲添加UILabel?
我目前可以获得播放的任何歌曲,并获得一首歌曲信息的UILabel,但当歌曲更改时,新歌的信息不会添加到uitable中的新单元格中。每次播放新歌时,如何将新歌信息添加到新单元格中Ios 如何为每首歌曲添加UILabel?,ios,swift,Ios,Swift,我目前可以获得播放的任何歌曲,并获得一首歌曲信息的UILabel,但当歌曲更改时,新歌的信息不会添加到uitable中的新单元格中。每次播放新歌时,如何将新歌信息添加到新单元格中 override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPl
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
}
您可以注册MPMusicLayerControllerNowPlayingItemDidChangeNotification通知,如中所述 然后在通知选择器中,调用getNowPlayingItem将新歌添加到表中 您还必须使用新歌曲信息创建一个新的播放对象,并将其添加到self.add数组中
let name = title + "\n" + artist + "\n" + album + "\n";
self.add.append(Play(name: name));
我已经这样做了,但是没有添加另一个标签,请查看更新的版本。添加项目后,您必须在表上调用reloadData。reloadData将一个uilabel替换为新歌,但我希望为新单元格中的每首歌添加一个新uilabel。而不是self.add=[播放(名称:“(标题!)\n(艺术家!)\n(专辑!)”,您应该将新的播放对象添加到self.add数组中。我没有新的播放对象