问题描述
我有一个带有静态单元格的tableView。 modalCell(请参见屏幕快照)与tableViewController
模态呈现的viewController保持联系。如果我关闭模式视图,则取消选中该单元格。但是,当我将单元格滚动出屏幕并向后滚动以使其再次出现时,再次选择了该单元格。我在一个新的示例项目中重现了这种行为。
我在ViewController
中的代码如下:
class ViewController: UITableViewController {
@IBOutlet weak var modalCell: UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath),cell == modalCell {
modalCell.setSelected(false,animated: true)
}
}
}
在屏幕录像中,您可以看到该单元格已正确取消选择。但是,如果我滚动,则再次选择该单元格。有什么想法吗?
解决方法
只需在表格视图中取消选择单元格
override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath,animated: true)
}