【发布时间】: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