gpt4 book ai didi

ios - didSelectRowAt 不适用于我的 UITableViewController

转载 作者:行者123 更新时间:2023-11-29 05:43:17 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com