每次我尝试访问UITableViewCell的textLabel时都会返回Nil



>我正在尝试在我的

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
let cellLabel = selectedCell!.textLabel!.text
print(cellLabel)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let musicCell = music[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "MusicCell") as! MusicCell
cell.setMusic(music: musicCell)
return cell
}

但它返回的尼尔。

MusicListView是我的UIViewController,我已经扩展了它

extension MusicListView: UITableViewDataSource, UITableViewDelegate {
}

任何帮助将不胜感激:)

因此,按照其他人的建议,我从数据模型中访问了所需的数据。 我写了一个小函数来访问数据模型

func findMusic(id : Int) -> String {
currentTitle = songList[id].title!
return currentTitle
}

并在每次用户点击单元格时调用它

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentTitle = findMusic(id: indexPath.row)
playSong(title: currentTitle)
}

最新更新