【问题标题】:why does 'didSelectRowAt indexPath' function only run after a two-finger tap?为什么“didSelectRowAt indexPath”函数只在两指点击后运行?
【发布时间】:2019-02-07 03:36:09
【问题描述】:

我的表格视图自定义单元格行有 3 个元素:嵌入在 UIScrollView 中的 UITextField、标签和按钮。 'didSelectRowAt indexPath' 函数仅在同时点击任何 2 个 3 行元素时运行。为什么是这样?我没有添加任何触摸手势。

可能有用的信息:tableview delegate 和 dataSource 设置为 self。删除 tableView.reloadData() 将导致代码更改不会反映在 UI 中。 self.tableView.beginUpdates() & self.tableView.endUpdates() 无效。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("test")
    if dataSource.data[indexPath.row].isDataCollapsed() {
        print("expanded")
        dataSource.data[indexPath.row].isCollapsed = false
        tableView.reloadData()
    }
}

提前谢谢你!

编辑:

由于我无法弄清楚,我求助于使用触摸手势。谢谢。

【问题讨论】:

  • 尝试在主线程上运行这段代码
  • 通过为 UITextField 和 UIButton 提供 'isUserInteractionEnabled = false' 来禁用触摸事件。
  • @JarvisTheAvenger 我该怎么做? @AshokPolu,感谢您的推荐,但是没有用。还有其他建议吗?
  • 我认为你应该首先通过禁用 UI 元素用户交互来尝试它。
  • @MansurAhmed 遇到同样的问题,你解决了吗?

标签: swift tableview


【解决方案1】:

好吧,我终于想通了。原来是这样的

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

不能与现有的自定义点击手势协同工作(不确定它是否仅限于点击手势或一般手势)。这意味着您不能在与 tableView 相同的视图控制器 (VC) 中同时激活点击手势,否则您的 didSelectRowAt 函数将无法正确响应触摸。

我想出了两种解决方案。一个是一种解决方法:像我之前所做的那样添加一个自定义点击手势,并且如上面@George_E 的答案所示(或类似于取决于您的用例),以让您的 tableView 响应 DidSelectRowAt。或者,您可以使用 tableView 活动在视图中使用其他预先存在的自定义点击手势。我的意思是当您的 tableView 处于活动状态并且需要一些点击手势交互时,禁用自定义点击手势。相反,当 tableView 处于非活动状态或不需要点击手势交互时,重新启用您的自定义点击手势。将以下代码放置在您想要禁用/启用自定义点击手势的任何位置:

for gesture in (self.view?.gestureRecognizers)! {
        if gesture.isKind(of: UITapGestureRecognizer.self) {
            gesture.isEnabled = false // or true if you want to enable it
        }
    }

【讨论】:

  • 另外,如果您更改了allowsSelection = false,请务必删除它!
  • 事实证明,现在我的表格视图和点击手势可以同时工作,当它们应该被禁用时。我想你应该一次只保持一个点击手势。 ?
【解决方案2】:

过时 - 请参阅已接受的答案


原答案

我遇到了同样的问题,也无法解决。所以我决定添加一个点击手势,它也像以前一样动画。

在我didMoveToSuperview的表内:

// Set delegates
delegate = self
dataSource = self

// Setup cells
register(MyCustomTableCell.self, forCellReuseIdentifier: "cellId")
allowsSelection = false // <-- Stops the user from tapping with 2
                        //     fingers, creating a permanent selection.

表格视图中设置单元格的函数(记得用下面的自定义类初始化):

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Create cell
      let cell = MyCustomTableCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "cellId")
     /* OR */
      let cell = dequeueReusableCell(withIdentifier: "cellId")
    
    // Setup cell
    /* ... */

用于表格单元格的自定义类以获取我们想要的内容:

final class MyCustomTableCell: UITableViewCell {
    
    override func didMoveToSuperview() {
        // Create the tap gesture
        let gesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(gesture:)))
        addGestureRecognizer(gesture)
    }
    
    @objc private func tapGesture(gesture: UITapGestureRecognizer) {
        setSelected(true, animated: true)
        setSelected(false, animated: true)
        print("Tap!")
    }
}

希望有人觉得这很有用!最好避免使用变通方法,但在这种情况下是必需的。 ?

【讨论】:

  • 我知道为什么 didSelectRowAt 函数对我不起作用。请参阅下面的答案以获取无需添加额外点击手势的解决方案。希望对您有所帮助!
猜你喜欢
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多