通常,当我触摸 UITableViewCell 时,会选择并突出显示 UITableViewCell。
但是,再次触摸完全相同的 UITableViewCell,然后什么也没有发生。
我希望如果我触摸选定的 UITableViewCell,然后取消选择 UITableVIewCell。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else { return }
if cell.isSelected == true {
cell.isSelected = false
}
}
Run Code Online (Sandbox Code Playgroud)
/////
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
}
}
Run Code Online (Sandbox Code Playgroud)
两个源代码都不起作用。我该如何解决这个方法?
Mil*_*sáľ 10
最小工作示例(前 7 个单元格是可选的):
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3750 次 |
| 最近记录: |