【问题标题】:tableView editing mode delete button (minus sign) not responding to tap - how do I fix this?tableView 编辑模式删除按钮(减号)没有响应点击 - 我该如何解决这个问题?
【发布时间】:2019-02-15 04:05:50
【问题描述】:

当我的 tableView 进入编辑模式时,单元格的每一侧都有一个图标:右侧的图标是重新排列单元格顺序的默认图标,左侧的图标是删除单元格的默认图标.我可以轻松地与重新排列图标进行交互,没有任何问题,但是,点击删除图标没有响应。它仅在我笨拙地向右滑动然后向左滑动时才有效。下面我将我认为与我的情况相关的所有代码都放在了下面。

一些背景信息:我在视图中有三个触摸手势(单击、双击和长按)。我尝试使用启用编辑模式的条件禁用它们。我什至尝试过事先删除触摸手势,但这并没有帮助。我认为这可能是一个自动布局问题,没有为删除按钮注册区域,但是重新排列按钮可以工作,所以这不应该吗?我的单元格的前导和尾随常数都是 +10 的视图。此外,isEditing 由 UIButton 控制。最后,我将 tableView 委托设置为 self,并在启用编辑时进行触摸交互。

任何帮助将不胜感激。谢谢!

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if isEditing { return true }
    return false
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    if isEditing { return .delete }
    return .none
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        countBeforeDeletingCell =  dataSource.data.count
        dataSource.data.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .none)
    }
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedObject = dataSource.data[sourceIndexPath.row]
    dataSource.data.remove(at: sourceIndexPath.row)
    dataSource.data.insert(movedObject, at: destinationIndexPath.row)
    tableView.reloadData()
}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    嘿,如果您使用 UITableViewController 并使用 isEditing iOS 默认模式,则必须覆盖 setEditing 模式和更改编辑按钮单击。

    • editButtonItem 中的 iOS self.navigationItem.rightBarButtonItem = self.editButtonItem 是默认的 UIViewController 方法。 editButtonItem 按钮操作覆盖。

    Swift 示例:

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.reloadData()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 2017-02-11
      • 2020-03-25
      • 2020-02-14
      相关资源
      最近更新 更多