我有一个表格视图,其中一个表格视图单元格打开另一个应用程序。当我返回我的应用程序时,表格视图单元格仍然突出显示。返回应用程序时取消选择表格视图单元格的最佳方法是什么?
编辑:问题是-viewWillAppear
or -viewDidAppear
从应用程序返回时不会被调用,因为视图已经可见。
在 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 文档
- UIApplicationDidBecomeActive https://developer.apple.com/reference/foundation/nsnotification.name/1622953-uiapplicationdidbecomeactive
- UITableView-indexPathForSelectedRow https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow
- UITableView - 取消选择行 https://developer.apple.com/reference/uikit/uitableview/1614989-deselectrow
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)