简体   繁体   English

tableView 编辑模式删除按钮(减号)不响应点击 - 我该如何解决这个问题?

[英]tableView editing mode delete button (minus sign) not responding to tap - how do I fix this?

When my tableView goes into editing mode there's an icon on each side of my cell: the icon on the right is the default icon for rearranging the order of cells, and the icon on the left is the default icon for deleting a cell.当我的 tableView 进入编辑模式时,我的单元格的每一侧都有一个图标:右边的图标是重新排列单元格顺序的默认图标,左边的图标是删除单元格的默认图标。 I can easily interact with the rearranging icon with no problems, however, tapping on the delete icon gives no response.我可以轻松地与重新排列图标进行交互,没有任何问题,但是,点击删除图标没有任何反应。 It only works when I do an awkward right swipe then left swipe on it.它仅在我向右滑动然后向左滑动时才有效。 Below I've placed what I believe to be all the relevant code to my situation.下面我放置了我认为与我的情况相关的所有代码。

Some background information: I have three touch gestures in the view (a single tap, double tap, and long press).一些背景信息:我在视图中有三种触摸手势(单击、双击和长按)。 I've tried disabling them with conditions for when the editing mode is enabled.我已经尝试在启用编辑模式的条件下禁用它们。 I've even tried deleting the touch gestures beforehand, but that didn't help.我什至尝试过事先删除触摸手势,但这没有帮助。 I thought it might be an auto layout issue that's not registering the area for the delete button, but the rearranging button works so shouldn't this?我认为这可能是一个自动布局问题,没有为删除按钮注册区域,但重新排列按钮有效,所以不是吗? Both the my cell's leading and trailing constants are +10 to the view.我的单元格的前导常数和尾随常数都为视图的 +10。 Furthermore, isEditing is controlled by a UIButton. Finally, i have the tableView delegate set to self, and touch interaction when editing is enabled.此外,isEditing 由 UIButton 控制。最后,我将 tableView 委托设置为 self,并启用编辑时的触摸交互。

Any help would be appreciated.任何帮助,将不胜感激。 Thank you!谢谢!

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()
}

Hey If the you have use UITableViewController and using isEditing iOS defalut mode you have to override setEditing Mode and Changing Edit button Click. 嘿,如果您已经使用UITableViewController并使用isEditing iOS默认模式,则必须覆盖setEditing Mode和Change Edit按钮单击。

  • iOS self.navigationItem.rightBarButtonItem = self.editButtonItem in editButtonItem is default UIViewController method. iOS的editButtonItem self.navigationItem.rightBarButtonItem = self.editButtonItem是默认的UIViewController方法。 editButtonItem button Action override. editButtonItem按钮操作覆盖。

Swift Example: Swift示例:

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

it works for me on swift 5.7.May be you should config TrailingSwipeActionConfiguration like this:它适用于 swift 5.7。也许你应该像这样配置 TrailingSwipeActionConfiguration:



unc tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let deleteAction = UIContextualAction(style: .destructive,
                                                  title: "Delete") { [weak self] _, _, complete in
                //remove row in you data model here
                tableView.deleteRows(at: [indexPath], with: .automatic)
                complete(true)
            }
            deleteAction.backgroundColor = .red

            return UISwipeActionsConfiguration(actions: [deleteAction])
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM