OGeek|极客世界-中国程序员成长平台

标题: ios - 返回应用程序时取消选择表格 View 行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:00
标题: ios - 返回应用程序时取消选择表格 View 行

我有一个表格 View ,其中一个表格 View 单元格打开了另一个应用程序。当我返回我的应用程序时,表格 View 单元格仍然突出显示。返回应用程序时取消选择表格 View 单元格的最佳方法是什么?

编辑:问题是 -viewWillAppear-viewDidAppear 在从应用程序返回时不会被调用,因为 View 已经可见。



Best Answer-推荐答案


在 viewDidLoad 中设置通知

final override func viewDidLoad() {
    super.viewDidLoad()

    // add notification observers
    NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

创建方法 didBecomeActive

func didBecomeActive() {
    if let indexPath = tableView.indexPathForSelectedRow {
        deselectRow(at: indexPath, animated: true)
    }
}

UIKit 文档

关于ios - 返回应用程序时取消选择表格 View 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473220/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4