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被调用之前检查它是否已经改变 –