【问题标题】:Swift 4: didSelectRowAt delegate not being calledSwift 4:没有调用 didSelectRowAt 委托
【发布时间】:2018-05-26 17:48:00
【问题描述】:

我知道发布了很多这样的问题,但我不知道为什么我的表委托方法没有被调用。

我正在声明协议 ~ UITableViewDelegate 和 UITableViewDatasource

在 Storyboard 中,我将 tableview 的委托和数据源设置为我的 UIViewController。

我有一个自定义 UITableViewCell。检查了那个和tableview,都检查了UserInteractionEnabled。

数据源方法有效,表格填充了正确的数据。

这是我的 didSelectRowAt 方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath.row)
}

这永远不会打印,我在那里的断点永远不会被击中。

【问题讨论】:

  • willSelectRowAtIndexPath 是否被调用?
  • 不,它也不会被调用。
  • 您确认您的委托在您的表格视图中是非空的吗?假设已设置,表格视图是否处于编辑模式?如果 isEditing 为 true,则不会调用 Delete 方法,除非 allowSelectionDuringEditing 也为 true。
  • 您的视图上是否有任何手势识别器可以接收触摸?
  • 这个问题可能在 IB 中与 UITableView 的属性有关:选择设置为无选择

标签: uitableview delegates


【解决方案1】:

试试看:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  let cell = tableView.cellForRow(at: indexPath)
  print("your content = \(String(describing: cell?.textLabel?.text))")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多