2013-05-20 93 views
1

如何通過推送通知通過NSNotification獲取表視圖?通過通知更新Tableview

當我的應用程序收到通知時,通過應用程序委託的- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo方法和應用,目前已經在運行,我更新數據(通過核心數據存儲)

查看警報彈出,一旦被駁回,我請

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateConversations" object:nil]; 

在表格視圖控制器:

- (void)viewDidLoad { 
    //some setup code removed 
    _someData = [[GGGroups findAllSortedBy:@"lastUpdated" ascending:NO withPredicate:[NSPredicate predicateWithFormat:@"ANY users = %@", [GGUser currentUser]]] mutableCopy]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData:) name:@"updateConversations" object:nil]; 
} 

- (void)updateData:(NSNotification *)notification { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableView reloadData]; 
     NSLog(@"Updated those conversations"); 
    }); 
} 

我曾嘗試添加和移除噸他dispatch_async主隊列塊。

它總是到達updateData方法(我可以看到NSLog),tableview本身永遠不會更新。

我在這裏做錯了什麼?


更新

更多的代碼的要求。

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [_someData count]; 

} 

此外,每個表視圖單元格都配置爲_someData的索引。

appDelegate更新GGGroups的核心數據結構,所以tableview應該相應地更新,但這不會發生,因爲我迄今爲止未知的原因。

+1

NSLog的發生,對不對? – Undo

+3

你確定你在IB中正確鏈接了tableView嗎? – Pei

+0

顯示如何填充表格。 –

回答

0

你說你正在使用核心數據,但你實際上並沒有觀察到那裏的變化(比如通過使用提取的結果控制器)。您正在設置_someData,但在模型更改時不會更新它。

嘗試更新用來填充表視圖中的數據時,您會收到通知:

- (void)updateData:(NSNotification *)notification { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     _someData = [[GGGroups findAllSortedBy:@"lastUpdated" ascending:NO withPredicate:[NSPredicate    predicateWithFormat:@"ANY users = %@", [GGUser currentUser]]] mutableCopy]; 
     [self.tableView reloadData]; 
     NSLog(@"Updated those conversations"); 
    }); 
} 
+0

我以前試過這個,但它只是空白了整個tableView出於某種原因 – GangstaGraham

+0

這表明問題確實與findAllSortedBy或GGUser currentUser ... – Wain

+0

我的heroku服務器再次工作,所以我會通過使用斷點來看看它是怎麼回事,findAllSortedby可能是正確的,但因爲它是GitHub – GangstaGraham