Ios 如何在UITableView中取消选择所选单元格
通常,当我触摸UITableViewCell时,会选择并高亮显示UITableViewCell 但是,再次触摸完全相同的UITableViewCell,则不会发生任何事情 我希望,如果我触摸选定的UITableViewCell,然后触摸UITableViewCell以取消选择Ios 如何在UITableView中取消选择所选单元格,ios,uitableview,Ios,Uitableview,通常,当我触摸UITableViewCell时,会选择并高亮显示UITableViewCell 但是,再次触摸完全相同的UITableViewCell,则不会发生任何事情 我希望,如果我触摸选定的UITableViewCell,然后触摸UITableViewCell以取消选择 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let cell
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else { return }
if cell.isSelected == true {
cell.isSelected = false
}
}
/////
两个源代码都不起作用。如何修复此方法?最小工作示例(前7个单元格可选):
如果需要multiselect,则必须保留选定IndExpath的数组。。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else { return }
if cell.isSelected == false {
cell.isSelected = true
} else {
cell.isSelected = false
}
}
import UIKit
import PlaygroundSupport
class MyTableViewController: UITableViewController {
var selectedIndexPath: IndexPath? = nil
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPath == indexPath {
// it was already selected
selectedIndexPath = nil
tableView.deselectRow(at: indexPath, animated: false)
} else {
// wasn't yet selected, so let's remember it
selectedIndexPath = indexPath
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyTableViewController()