Swift-如何访问单元格名称
所以我在做音乐播放器应用程序,但我不知道如何播放当前歌曲。我把所有歌曲排成一行(标识符是迈塞尔)。当我单击名为的单元格时,例如,Starboy“它应该得到这个名字,并将数据传递给另一个VC,然后在那里播放,但它总是只播放原始的最新内容,因此,例如,有3首歌曲:Old Town Road,Starboy,Hello,它只能播放最新的内容,因此在本例中,它是Hello。有人能帮我吗?感谢阅读。您需要实现tableview的委托方法Swift-如何访问单元格名称,swift,xcode,uitableview,Swift,Xcode,Uitableview,所以我在做音乐播放器应用程序,但我不知道如何播放当前歌曲。我把所有歌曲排成一行(标识符是迈塞尔)。当我单击名为的单元格时,例如,Starboy“它应该得到这个名字,并将数据传递给另一个VC,然后在那里播放,但它总是只播放原始的最新内容,因此,例如,有3首歌曲:Old Town Road,Starboy,Hello,它只能播放最新的内容,因此在本例中,它是Hello。有人能帮我吗?感谢阅读。您需要实现tableview的委托方法didSelectRowAt,每当用户点击表格视图的任何一行时,该方法
didSelectRowAt
,每当用户点击表格视图的任何一行时,该方法将更新songName
属性
import UIKit
class TableViewControllerOne: UITableViewController {
let musicArray = ["Old Town Road", "Starboy"]
var songName = ""
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return musicArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.textLabel!.text = musicArray[indexPath.row]
//here is the problem
songName = cell.textLabel!.text!
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! ViewControllerOne
vc.songFinalName = self.songName
}
}
此外,删除
songName=cell.textLabel!。短信
fromcellForRowAt:indepath
method显示您的didSelectRow方法为什么不简单地设置vc.songFinalName并显示视图控制器?我想让他对现有代码做最小的更改。我同意触发segue的最好方法应该是从didSelect开始,如果你没有什么大的事情需要处理的话。所以,谢谢你的回答!它有点工作,但它播放的音乐,我希望它播放,然后一个随机音乐一起。我只能停止,、random“音乐。我从cellForRowAt:indexPath中删除了songName=cell.textLabel!.text!,并添加了override public func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){songName=musicArray[indexPath.row]//性能模式(标识符为:“dataTransport”,发送者:self)}当我添加performsgue时…它会播放两首歌曲。如果没有它,它只会播放随机歌曲。你知道问题出在哪里吗?谢谢!你如何执行到ViewControllerOne的转换以及如何将歌曲分配给ViewControllerOne的songFinalName属性似乎有问题。你能分享你的performsgue co吗de和VIEWCONTROLLENER代码
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
songName = musicArray[indexPath.row]
}