didSelectRowAt не работает с моим UITableViewController - PullRequest
0 голосов
/ 30 мая 2019

У меня проблемы с моим UITableVieController.Неправильно вызывать мою функцию didSelectRowAt.У меня есть несколько разделов в моем UITableView, но другой раздел моего кода с таким же точным кодом работает совершенно нормально, и я не могу понять, почему это не работает

Я уже проверил, имеет ли мое табличное представление правильноеделегат и источник данных, и это делает.

 override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 1 : songList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell", for: indexPath) as! InfoCell

            cell.playlistImage.image = playlistImage
            cell.name.text = selectedPlaylist
            cell.nuberOfSongs.text = "Number of Songs: \(playlistCount)"

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SongCell", for: indexPath) as! SongCell
            cell.SongTitle.text = songList[indexPath.row]
            cell.SongArtist.text = artistList[indexPath.row]
            cell.SongImage.image = imageList[indexPath.row]
            return cell
        }
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if indexPath.section != 0 {

            selectedItem = mediaList[indexPath.row]

            play(selectedItem: selectedItem)

            performSegue(withIdentifier: "showPlayer", sender: self)

            tableView.deselectRow(at: indexPath, animated: true)
        }

    }

Это весь мой код для создания разделов и строк, которые идут в этих разделах.Он также содержит код для didSelectRowAt, но эта функция вообще не вызывается.

1 Ответ

0 голосов
/ 30 мая 2019

Предоставляете ли вы UITapGestureRecogniser для своего суперпредставления UITableView, если это так, то метод didSelectRowAt UITableview не будет работать, потому что ваше суперпредставление получает касание вместо ячейки табличного представления.

Для этого вы можете добавить делегата в ваш UITapGestureRecogniser следующим образом:

let tap = UITapGestureRecognizer(target: self, action: #selector(selectorFunction))
tap.delegate = self
superview.addGestureRecognizer(tap)

После этого просто настройте ваш viewcontroller для этого делегата

extension YourViewController : UIGestureRecognizerDelegate
{
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if (touch.view?.isDescendant(of: yourTableView))!
        {
            return false
        }
        return true
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...