【问题标题】:tableview didSelectRowAtIndexPath cannot detect any clicktableview didSelectRowAtIndexPath 无法检测到任何点击
【发布时间】:2017-08-18 06:07:51
【问题描述】:

我创建了一个 UITableView 并将其连接到数据源并委托故事板并将 UITableViewDelegateUITableViewDataSource 放在课堂上。当然还有UINavigationControllerDelegate 推送到另一个UIViewcontroller

我打过电话

self.tableView.dataSource = self
self.tableView.delegate = self

viewDidLoad.

这是我的didSelectRowAtIndexpPath: 代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //let cell = tableView.dequeueReusableCellWithIdentifier(reuseContentFreeIdentifier, forIndexPath: indexPath) as! FreeTableViewCell
    print("didSelectRowAtIndexPath eshops")
    let row = indexPath.row
    print("business row:\(HCPartner[row])")
    performSegue(withIdentifier: "eshopsURL", sender: self)
}

当我点击一个单元格行时,它没有检测到任何点击,也没有显示任何print("didSelectRowAtIndexPath eshops") 的日志。

为什么didSelectRowAtIndexPath: 不起作用?我在 swift 2 上试过,效果很好。但在 swift 3 中它不起作用。

我的代码有什么问题?什么是让它检测点击UITableView的单元格行的正确代码?

我的视单元层次结构如下:

【问题讨论】:

  • 你能贴一张你的单元格视图层次结构的图片吗?
  • 已经更新了单元格视图层次结构的图像
  • 打开您的单元格视图层次结构以查看您的单元格内的内容,如果我不够清楚,请见谅
  • 更新已发送的有问题的图片
  • 您的 tableView 显示您的数据?只是缺少选择?

标签: ios uitableview swift3


【解决方案1】:

确保您没有添加任何手势并且已启用 UserInteraction。

【讨论】:

  • 启用用户交互
  • 你在tableview中使用手势吗?
  • 我使用标准的表格视图。 swift 3中的标准tableview是否需要添加手势?在 swift 2 中,标准 tableview 与委托和数据源一起正常工作。
  • 否,但不能在 tableview 中使用手势,如果使用,则 didselect 将不起作用,另一种解决方案是使用框架等于单元格的按钮。你可以像这样触发按钮:- cell.btn.tag = indexPath.row cel.btn.addTarget(self, action: #selector(self.btnActionForCell(sender:)), for: .touchUpInside)
  • 哇,你纠正了这个错误。谢谢@Pushpendra Mishra
【解决方案2】:

请在Class中添加TableView Delegate和Datasource ->

类 ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { }

【讨论】:

  • 你的其他Delegate方法是否调用了:numberOfRowsInSection、CellForRowAtIndexpath??
  • 其他委托方法有: func numberOfSectionsInTableView(tableView: UITableView) -> Int , func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int, func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 。这个其他委托方法工作正常。
  • Swift 3 修改了很多方法的方法签名: Replace: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) 与 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 可能是这对你有帮助。
  • @Sarimin 这不会解决您的 didSelectRowAt 问题,但您的 numberOfSections 方法的方法签名错误。
  • @rmaddy.. 感谢您纠正我的答案 :) 这是正确的语法。 func numberOfSections(in tableView: UITableView) -> Int { return 0 }
【解决方案3】:

检查您的 tableView 是否实际连接到 IBOutlet。我怀疑viewDidLoad 中的self.tableView 实际上是零。

另外,请检查您的故事板以确保实际单元格具有“默认”选择样式。您可能不小心将选择样式设置为 .none

它应该看起来像这样:

【讨论】:

  • tableview 选择 -> 单选。表格视图单元选择->默认。已经像往常一样设置了,但是直到我在这里提出问题才起作用。
  • tableview 已经像往常一样作为引用插座连接到类,但没有工作。
猜你喜欢
  • 2021-04-15
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
相关资源
最近更新 更多