[英]Update tableView row from AppDelegate Swift 4
[![在此處輸入圖片描述] [1]] [1]
你好。 我具有如上圖所示的表格視圖,並且收到一些靜默的推送通知。 根據它們,我需要從tableView
重新加載特定的單元格。 因為我在AppDelegate
收到notification
,並且此刻正在重新加載整個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
用於填充我的表格視圖
讓應用程序代理在主線程上廣播特定於應用程序的通知中心通知。 讓包含表視圖的視圖控制器偵聽該通知,並根據需要更新有問題的單元格。 這樣,您就不會污染應用程序委托。 應用程序委托僅應處理系統級應用程序內容,而不應處理業務邏輯。
您可以使用self.mainTableView.cellForRow(at:IndexPath(…)
獲取行的單元格,然后直接對其進行更新。
或者,我發現您節省了時間, 並且使用ALTableViewHelper [商業-可以在Framework Central上使用 在這里 ]。 免費試用。 該助手將完成大部分工作-您將描述數據如何連接到UITableView。 我整理了一個示例(在GitHub上的此處 ),希望該示例類似於您要嘗試執行的操作。
import ALTableViewHelper
class VoiceViewController {
// @objc for the helper to see the var’s, and dynamic so it sees any changes to them
@obj dynamic var conversations: Any?
@obj dynamic var onlineRequest: Any?
func viewDidLoad() {
super.viewDidLoad()
tableView.setHelperString(“””
section
headertext "Conversation Status"
body
Conversation
$.viewWithTag(1).text <~ conversations[conversations.count-1]["title"]
$.viewWithTag(2).text <~ "At \\(dateFormat.stringFromDate(conversations[conversations.count-1]["update"]))"
UpdateButton
$.viewWithTag(1).isAnimating <~ FakeConversationGetter.singleton.busy
“””, context:self)
}
func getConversations() {
let reachabilityManager = NetworkReachabilityManager()
if (reachabilityManager?.isReachable)! {
ServerConnection.getAllConversation { (data) in
if let _ = data {
// change the data on the main thread as this causes the UI changes
GlobalMainQueue.async {
self.conversations = data
self.onlineRequest = self.conversations
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.