0

我的問題是,我得到本地通知,然後告訴我的一個VC更新其UITableView。但是,tableview不會重新加載。我知道所有if語句檢查都通過並且正在運行代碼。這原本讓我相信這是一個線程問題,所以我在dispatch_async(dispatch_get_main_queue(), {})中包裝了重新加載。但是這也不起作用。所以如果不是線程問題是什麼呢?什麼可能導致這個?我在這裏做錯了什麼?無法從didReceiveLocalNotification重新加載UITable

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    if notification.alertBody != nil{ 
     if notification.alertBody!.hasSuffix("starting") || notification.alertBody!.hasSuffix("ending"){ 
      dispatch_async(dispatch_get_main_queue(), { 
       if topMostContoller() is SecondViewController{ 
        let vc = self.tabBarController?.getVC(.Attendance) as! SecondViewController 
        vc.myTableView?.reloadRowsAtIndexPaths((vc.myTableView?.indexPathsForVisibleRows)!, withRowAnimation: .None) 
    //    vc.myTableView?.reloadData() <- this doesn't work either 
       } 
      }) 
     } 
    } 
} 

編輯:

func getVC(vc:tabBarVC)->UIViewController?{ 
    switch vc{ 
    case .Home: 
     return fullTabBar[0] 
    case .Attendance: 
     return fullTabBar[1] 
    case .Location: 
     return fullTabBar[2] 
    case .Sign: 
     return fullTabBar[3] 
    case .Settings: 
     return fullTabBar[4] 
    } 
} 

凡fullTabBar中的TabBar的viewDidLoad設置。

fullTabBar = self.viewControllers as [UIViewController]!

self是使用TabBar實例。

+0

你能打印調用reload之前'vc.myTableView'的值? –

+0

@PhillipMills'vc =可選(; layer = ; contentOffset :{0,0}; contentSize:{768,2728}>)' – boidkan

+0

其他代碼,例如爲UITableView填充數據數組,運行但實際上並不會影響它們應該使用的對象。我忽略了這個問題儘可能簡單化。 – boidkan

回答

0

我不幸看不到你的代碼有什麼問題,在邏輯上它應該工作。可能你的數據源在reloadData被調用後正在更新,儘管考慮到代碼的清潔程度,我懷疑是這種情況。

我會嘗試以下模式,如果只是爲了看看您的答案中的代碼是否有問題。

在SecondViewController的viewDidLoad:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTableView:", name: "reloadSecondVCTableView", object: nil) 

和身體:

func reloadTableView(_: NSNotification) { 
    tableView.reloadData() 
} 

和之後的第二,如果你發佈代碼語句發送通知:

NSNotificationCenter.defaultCenter().postNotificationName("reloadSecondVCTableView", object: nil) 
+0

已經嘗試過。沒有工作...''( – boidkan

+0

啊,該死!嗯,這個問題必須在其他地方,你如何更新你的tableView的數據源? –

+0

也許打印dataSource在其最初加載後,然後再次在您的代碼在reloadData被調用之前檢查它是否已經改變 –