首页 > 解决方案 > 从 AppDelegate Swift 4 更新 tableView 行

问题描述

[![在此处输入图像描述][1]][1]

你好。我有一个如上图所示的表格视图,并且我收到了一些无声推送通知。根据他们,我需要从tableView. 因为我现在notification正在AppDelegate重新加载整个tableView...但我个人认为这不是最好的解决方案,因为我只需要更新特定的行。

任何提示请我如何更新来自 appDelegate 的特定单元格?

if userInfo["notification_type"] as? String == "update_conversation" {
            if let rootVC = (self.window?.rootViewController as? UINavigationController)?.visibleViewController {
                if rootVC is VoiceViewController {
                    let chatRoom = rootVC as! VoiceViewController
                    chatRoom.getConversations()
               // the get Conversations method makes a call to api to get some data then  I reload the whole tableView
                }
            }


    func getConversations() {
    let reachabilityManager = NetworkReachabilityManager()
    if (reachabilityManager?.isReachable)! {
        ServerConnection.getAllConversation { (data) in
            if let _ = data{
                self.conversations = data
                self.onlineRecent = self.conversations
                GlobalMainQueue.async {
                    self.mainTableView.reloadData()
                }
            }
        }
    }
}

这是我的getConversation方法,用于VoiceViewController填充我的表格视图

标签: iosswiftuitableviewpush-notificationappdelegate

解决方案


让应用程序委托广播特定于应用程序的通知中心通知(在主线程上)。让包含您的表格视图的视图控制器侦听该通知并根据需要更新相关单元格。这样您就不会污染您的应用程序委托。应用程序委托应该只处理系统级应用程序的东西,而不是业务逻辑。


推荐阅读