UITableView 选定的单元格未取消选择

UITableView selected cell not getting deselect

我在 A ViewController 中有一个 searchController 和 tableView,我可以在其中搜索 tableView 中的任何项目和 select 该项目。我将所有 selected 项目保存在其他 ViewController B 中。 现在,当我再次重定向到 A VC,在那里我可以看到我的 selected 项目时,我想删除 select 来自 selected 单元格的一些项目,但我没有能够 deselect selected 细胞所有其他 deselected 细胞我可以 select 没有问题。 当我点击 selected 单元格去 select 时,我可以看到它没有调用下面的方法。

tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

在我的 viewDidLoad 中,我有以下属性

tableView.isEditing = true
tableView.allowsMultipleSelectionDuringEditing = true

而且我没有使用任何自定义单元格。如果您有任何解决方案,请帮助我。 TIA.

您可以使用 table 视图数据源模型控制选择。

假设您有一个对象数组,例如:var arr:[YourObject] = [] 然后将变量添加到 YourObject 例如:var isSelected: Bool = false

cellForRow方法中,检查您当前的项目选择状态并更改单元格选择样式。

最后,在 didSelectRow 方法中,更改您当前选择的对象 isSelected 变量状态,然后重新加载 table 视图

我已经解决了这个问题,这个问题与我设置单元格选择的方式有关我在 cellForRow 方法中添加了以下行。

    if cellItem.isSelected {
       tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    } else {
       tableView.deselectRow(at: indexPath, animated: true)
    }