Ноль возвращается каждый раз, когда я пытаюсь получить доступ к textLabel из UITableViewCell - PullRequest
0 голосов
/ 15 октября 2018

Я пытаюсь получить текст UITableViewCell в моем

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 {
}

Любая помощь будет принята с благодарностью:)

1 Ответ

0 голосов
/ 16 октября 2018

Так что, как предложили другие, я получил доступ к необходимым данным из модели данных.Я написал небольшую функцию для доступа к модели данных

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)
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...