【问题标题】:didSelectRowAt indexPath is not being called没有调用 didSelectRowAt indexPath
【发布时间】:2017-11-17 15:56:28
【问题描述】:

我创建了一个 UIViewController 包含两个视图(顶部,底部), 单击 searchBar 时,底部视图会一直扩展到顶部。 (bottomView高度扩大,topView高度变小)。

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    self.expandBottomView()
    return true
}

func expandBottomView() {
    let heightToAdd = TopView.frame.height - numOfRequestsTitle.frame.height

    RecomandFriendHeight.constant += heightToAdd
    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }

    topTableView.isUserInteractionEnabled = false
}

两个视图都包含 TableViews(topTableView,BottomTableView)所有代表都设置了我检查过。 BottomView 包含另一个按钮,可在需要时折叠底部视图。

topTableViewCell 包含两个按钮和两个标签。 bottomTableViewCell 包含一个标签和一个 imageView。

问题是 tableViews 单元格都没有调用 didSelectRowAt。

【问题讨论】:

  • 代表设置到什么类?
  • 到包含topView和bottomView的UIViewController。
  • 你能提供更多代码吗?我想看看为 tableViews 和 didSelectRowAt 方法声明设置的委托和数据源。
  • 尝试使用以下技术进行调试: 1. 尝试捕获视图层次结构并检查 tableView 是否可点击。 2. 确保在需要时启用 userInteraction,因为您正在禁用它。
  • 不是因为您实际上没有单击“单元格”而是单元格中的元素,它可能被标签或其他东西阻止了。调试视图层次结构

标签: ios iphone swift uitableview swift3


【解决方案1】:

制作
topTableView.isUserInteractionEnabled = true

【讨论】:

    【解决方案2】:

    如果您将按钮添加到单元格中,则在点击按钮时不会调用 didSelectAt。在这种情况下,您必须在 cellForRowAt 内的此按钮中添加一个选择器:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customcell
            cell.btn.addTarget(self, action: #selector(clickedCell), for: .touchUpInside)
            return cell
    
      }
    

    并实现这个选择器方法

    func clickedCell(_ sender : UIButton) {
            print("clicked")
     }
    

    【讨论】:

      猜你喜欢
      • 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-08-15
      相关资源
      最近更新 更多