UITableView在滚动时重新选择行

问题描述

我有一个带有静态单元格的tableView。 modalCell(请参见屏幕快照)与tableViewController模态呈现的viewController保持联系。如果我关闭模式视图,则取消选中该单元格。但是,当我将单元格滚动出屏幕并向后滚动以使其再次出现时,再次选择了该单元格。我在一个新的示例项目中重现了这种行为。

这是我的情节提要设置(没什么花哨的):

storyboard setup

我在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)
        }
    }
    
}

在屏幕录像中,您可以看到该单元格已正确取消选择。但是,如果我滚动,则再次选择该单元格。有什么想法吗?

enter image description here

解决方法

只需在表格视图中取消选择单元格

override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath,animated: true)
    }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...