首页 > 解决方案 > 连续左滑后 UITableViewCell 缺少删除控件

问题描述

我使用带有可编辑自定义样式单元格的表格视图。导航栏中有一个Edit按钮可以切换编辑模式。我需要编辑模式来删除和重新排序行。

当我向左滑动单元格时,Delete操作按钮会显示在该单元格的右侧。当它显示时,我按下Edit按钮,Delete操作按钮消失。再次按下该Edit按钮将显示删除命令左侧和所有(!)单元格右侧的重新排序控件。到目前为止一切都符合预期。

现在(例如),当我向左滑动单元格 0,然后Delete在单元格 0 中的操作按钮仍然可见时向左滑动单元格 1,使用Edit按钮激活编辑模式现在显示删除命令左侧和所有单元格的重新排序控制权,除了(!) 单元格 0。

我发现,只要左擦除 2 个或更多单元格,当使用Edit按钮启用所有单元格的编辑模式时,第一个未右滑退出编辑模式的单元格就会错过删除控件(左)。更奇怪的是……在主题单元格缺少删除控件(左)上,重新排序控件(右)显示正确!

我遵循并比较了许多教程,但没有发现错误。

我想念什么?

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    ...
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
}

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

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    ...
}

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    ...
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    ...
}

// not necessary = didn't change anything, but found in many tutorials
override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
}

@IBAction func editAction(_ sender: Any) { // that's the Edit button action
    self.setEditing(!isEditing, animated: true)
    self.navigationItem.leftBarButtonItem?.title = isEditing ? "Done" : "Edit"
}

最重要的是涉及的方法。不相关的方法内容被省略以保持帖子紧凑。如果认为需要,我将添加/编辑。

截图 1
向左滑动单元格 1 后查看(忽略RenameShare操作)。

截图 2
向左滑动单元格 2 后查看单元格 1,然后使用Edit按钮进入编辑模式。

标签: swiftuitableviewuitableviewrowaction

解决方案


嗨,我相信您的问题可以通过使用 UITableView 的预设来解决editButtonItem

navigationItem.leftBarButtonItem = self.editButtonItem

当执行滑动操作时,这会将按钮的状态设置为编辑模式,因此应该可以解决您遇到的问题。


推荐阅读