如何从应用程序委托自动重新加载 table 呈现的 table 视图控制器的视图单元格?

how to auto reload table view cells of presented table view controller from app delegate?

我正在创建一个应用程序,它每 30 秒触发一次本地通知,当我点击通知时,它会打开一个 table 视图控制器。 此 table 视图控制器在其 table 视图单元格上有一个徽章,用于显示通知计数(例如绿色图标中针对 whatsapp 中的联系人显示的消息数)。如果应用程序处于活动状态时通知到达(i.e.with table 屏幕上的视图控制器),则应在单元格的徽章上更新通知计数。我通过在我的应用程序委托的 didReceiveLocalNotification 方法中编写以下代码来做到这一点:

var root = self.window!.rootViewController as ViewController
let main: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setview = main.instantiateViewControllerWithIdentifier("tableview") as TableViewController

if application.applicationState==UIApplicationState.Active
    {
        TableViewController().tableView.reloadData()
        root.dismissViewControllerAnimated(true, completion: nil) //I don't want to use this. Only badges should be updated
        root.presentViewController(setview, animated:true , completion: nil)   
}

when I tap on the notification it opens a table view controller.

那么,为什么不仅在点击通知时才重新加载 table?

您应该在 ViewDidAppear 上填充单元格数组,然后在 UITableView 中显示更新后的单元格(带有徽章)。

或者(其他想法)- 当您的 UITableView 仍然打开时,为什么不使用 NSNotification 来更新您的 TableView?

类似于:

在您的 TableView 中 Class:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTable:", name:"ReloadHandler", object: nil)

    func reloadTable(notification: NSNotification){
       .... populate your cell data array
       ... reload your table

     }

// 在您的主视图控制器中(您接收 UIApplicationState 的地方)

NSNotificationCenter.defaultCenter().postNotificationName("ReloadHandler", object: false)