【发布时间】:2020-10-03 17:26:26
【问题描述】:
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
}
}
所以我正在做音乐播放器应用程序,但我不知道如何播放当前歌曲。我连续播放所有歌曲(标识符为 MyCell)。当我单击名为例如,Starboy 的单元格时,它应该获取该名称并将数据传递给另一个 VC 并在那里播放它,但它总是只播放原始的最新内容,例如有3 首歌曲:Old Town Road, Starboy, Hello,它只能播放最新的歌曲,所以在这个例子中是 Hello。有人可以帮我吗?谢谢阅读。
【问题讨论】:
-
显示你的 didSelectRow 方法
标签: swift xcode uitableview