ios - didSelectRowAt 不适用于我的 UITableViewController

标签 ios swift uitableview

我的 UITableVieController 遇到问题。它没有正确调用我的 didSelectRowAt 函数。我的 UITableView 中有多个部分,但我的代码的另一部分具有相同的确切代码,工作得很好,我不明白为什么这不起作用

我已经检查过我的 TableView 是否具有正确的委托(delegate)和数据源,确实如此。

 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 的代码,但根本没有调用该函数。

最佳答案

您是否向 UITableView 的 super View 提供 UITapGestureRecogniser ,如果是这种情况,则 UITableview 的 didSelectRowAt 方法将不起作用,因为您的 super View 正在接收触摸而不是表格 View 单元格。

为此,您可以向 UITapGestureRecogniser 添加委托(delegate),如下所示:

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

在此之后,只需使您的 View Controller 符合此委托(delegate)

extension YourViewController : UIGestureRecognizerDelegate
{
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if (touch.view?.isDescendant(of: yourTableView))!
        {
            return false
        }
        return true
    }
}

关于ios - didSelectRowAt 不适用于我的 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370147/

相关文章:

ios - 包含 UITableView 的 UITableViewCell 的高度(iOS8 和 AutoLayout)

ios - 如何在 SceneKit 中创建动画纹理? "fallback on default program"错误

swift - "if let "必须仅用于可选的? ( swift )

arrays - 为什么在数组扩展中使用 Stride 非法创建临时数组

ios - UITableView Cell 只需点击几下即可呈现 View Controller

Swift TableView Tab 发送错误的索引,而编辑发送正确的索引

iOS facebook 应用程序链接问题

objective-c - 获取发送到已释放实例的 [Not a type retain] 和 [Not a type class] 消息

ios - 多次播放后 iOS UIWebView 中 HTML5 视频的 MEDIA_ERR_DECODE

ios - 使用 Firebase 的最新版本 Xcode 出现错误?