【问题标题】:TableView _:didSelectRowAt: is triggered only on slide touchTableView _:didSelectRowAt: 仅在滑动触摸时触发
【发布时间】:2019-05-06 10:19:22
【问题描述】:

我展示了一个带有默认单元格的 UITableView 和带有 true 值的 isEditing/allowsMultipleSelection。 在 _:viewForHeaderInSection: 上,我设置了一个带有自定义委托的 CustomView,以获取有关此视图更改的反馈。

当用户点击一个单元格时,该单元格变为选中的颜色,但当用户释放单元格时,它变为未选中,此时_:didSelectRowAt:未被触发。

当用户点击单元格并做出滑动(点击、按住和移动)手势时,单元格将被选中并触发_:didSelectRowAt:

我需要正常的 tableView 行为(点击选择),多选。

我试图删除此视图上的所有其他委托(包括部分中标题的自定义视图),完全删除自定义标题视图,从我的 .xib 中删除 UITableView 并再次添加它,在设置 tableview 时更改位置,将isEditingallowsMultipleSelection 属性设置为false

设置表格视图

private func setupTableView() {
   self.tableView.delegate = self
   self.tableView.dataSource = self
   self.tableView.tableFooterView = UIView()
        
   self.tableView.isEditing = true
   self.tableView.allowsMultipleSelection = true
   self.tableView.allowsMultipleSelectionDuringEditing = true
}

tableview 委托和数据源

    @IBOutlet weak var tableView: UITableView!

    private let header = TableViewHeader()


    [...]


    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell()
        cell.textLabel?.text = dataSource[indexPath.row].title
        return cell
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        self.header.delegate = self
        return self.header.view
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let selectedRows = tableView.indexPathsForSelectedRows {
            if selectedRows.count > 5 {
                tableView.deselectRow(at: indexPath, animated: true)
            }
        }
    }

我确实从我的视图中删除了一个自定义 UITapGestureRecognizer,现在可以正常工作了!

【问题讨论】:

  • allowsMultipleSelectionallowsMultipleSelectionDuringEditing 设置为 false.. 不起作用?
  • 如果你在你的didSelectRowAt表视图委托方法中放置一个断点,selectedRows的类型和值是什么?
  • @NiravKotecha 我需要在 tableView 上进行多项选择,我尝试禁用它,但仍然无法正常工作
  • 您是否在该视图控制器中应用了点击手势?
  • @NiravKotecha 你是对的,我被添加了一个点击手势来查看 resignFirstResponder 并且它阻止了我在 tableview 上的点击手势。我删除了手势,现在可以正常工作了!

标签: ios swift uitableview


【解决方案1】:

请检查您是否在该视图控制器上应用了点击手势,因为它会阻止您在 tableView 选择中。

通过点击手势委托方法代码,您可以取消阻止您的 tableView 选择。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isDescendantOfView:YOUR_TABLE_NAME])
    {
        return NO;
    }
    return YES;
}

【讨论】:

  • 是的,这就是问题所在,我的视图有一个自定义的点击手势并且阻止了 tableview 的点击手势。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多