返回应用程序时取消选择表格视图行 [英] Deselect table view row when returning to app

查看:19
本文介绍了返回应用程序时取消选择表格视图行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table view and one of the table view cells opens another app. When I return to my app the table view cell is still highlighted. What is the best way to deselect a table view cell when returning to the app?

问题是 -viewWillAppear-viewDidAppear 在从应用程序返回时不会被调用,因为视图已经可见.

The issue is that -viewWillAppear or -viewDidAppear does not get called when returning from an app since the view is already visible.

推荐答案

在 viewDidLoad 中设置通知

Set notification in viewDidLoad

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

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

创建方法 didBecomeActive

Create method didBecomeActive

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

UIKit 文档

这篇关于返回应用程序时取消选择表格视图行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆