【问题标题】:Issue selecting all tableview cells at the same time | Swift同时选择所有表格视图单元格的问题 |迅速
【发布时间】:2019-11-08 04:43:12
【问题描述】:

基本上我希望能够在表格视图中选择所有单元格并取消选择所有单元格。

目前我正在使用:

for section in 0..<tableView.numberOfSections {
    for row in 0..<tableView.numberOfRows(inSection: section) {
        let indexPath = IndexPath(row: row, section: section)
        _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath)
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
        tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
    }
}

虽然此功能确实有效,但它同时充当全选和取消全选,这不是我们所需要的。相反,我需要它始终选择所有记录,如果已经选择了一些记录,则在执行此函数时应该忽略它们。它应该只选择那些尚未选择的记录。上面的函数如何修改为只选择行而不取消选择已经选择的行。

更新:

我在 didSelectRow 中执行以下操作:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    structure[indexPath.row].isSelected.toggle()
    let portfolio = structure[indexPath.row]
    updateSelection(of: portfolio, at: indexPath)
}

函数updateSelection只是一个使用Alamofire更新API的函数,

【问题讨论】:

  • 这能回答你的问题吗? Select all the cells in UITableView
  • 不完全是,这些方法都不会监视已选择的单元格。如果单元格已被标记为选中,则不应触摸它。
  • 基本上我需要它来拒绝取消选择,这可能吗?
  • @JonathanMense 所以您想在选择所有行后取消选择之前选择的行?这意味着假设您总共有 10 行,其中您选择了 3 行,现在您尝试单击一个按钮,该按钮将选择所有行,然后只应选择 7 行,而应取消选择其他 3 行...这是您想实现吗?

标签: ios swift uitableview


【解决方案1】:

刚刚在一个示例项目中尝试了以下方法,它可以按您的意愿工作:

func selectAll() {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            let indexPath = IndexPath(row: row, section: section)
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
        }
    }
}

func deselectAll() {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            let indexPath = IndexPath(row: row, section: section)
            tableView.deselectRow(at: indexPath, animated: false)
        }
    }
}

那是不是对你不起作用?

【讨论】:

  • 我一直在测试你的代码,我发现它在我的情况下不起作用,因为我也使用了复选标记附件..基本上逻辑应该是我拥有的代码,但是如果已存在复选标记不要选择它。你知道这是怎么做到的吗?
  • 需要查看您的代码,当前代码不会更改复选标记,您的单元格中可能有一个逻辑切换复选标记而不是查看isSelected
  • 好的,我添加了更多细节,更新后的帖子显示了我如何使用切换从选中状态切换到未选中状态。这有帮助吗?
  • 那么干脆不要使用toggle,而使用= true。你也可以,注意检查structure[indexPath.row].isSelected == true是否只是返回并且什么都不做
  • 感谢您的帮助,您能看看我对@vadian 回答的最后评论吗?我解释了有关该问题的更多详细信息
【解决方案2】:

根据您的说法,UPDATE 似乎表视图中只有一个部分,因此您的代码具有误导性。

一个简单的解决方案是枚举数据源数组并检查isSelected 是否具有所需的值。如果不切换值并更新 API。最后重新加载表格视图。使用true 调用函数将选择所有单元格,false 将取消选择单元格。

func changeSelection(_ flag : Bool) {
    for (index, portfolio) in structure.enumerated() {
        if portfolio.isSelected != flag {
            structure[index].isSelected.toggle()
            updateSelection(of: structure[index], at: IndexPath(row: index, section: 0))
        }
    }
    tableView.reloadData()
}

旁注:

切勿自己调用包含willdidshould 的委托方法。

【讨论】:

  • 对不起,我不明白在哪里可以选择让功能只选择还是只取消选择?你提到用真假调用函数,这怎么办?我需要该函数只选择所有行,如果它们已经被选中,请不要取消选择它们。
  • 只需调用changeSelection(true) 选择所有未选中的单元格,调用changeSelection(false) 取消选择所有选中的单元格。
  • 唯一的另一个问题是在记录更改之前应用复选标记,在使用 updateSelection 函数在 API 中进行更改之前,复选标记不应该出现。
  • 我帖子中的原始函数,是不是...for section in 0..&lt;tableView.numberOfSections {这如何应用于您的函数?
  • 1) 从更新 API 返回后重新加载每一行。 2)正如我所说,根据didSelect中的代码,只有一个部分。
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多