這似乎很荒謬,但我似乎無法將其整理出來。我需要在表視圖中插入一行以響應推送通知。實施應是越簡單從代碼中插入UITableView行
- (void)didSaveMessage:(Message*)message atIndex:(int)index
{
//check to make sure I have an array to populate the table from...
if (self.messageManager.messages != nil)
{
self.indexRow = index;
NSLog(@"refresh table view from code");
//remove a label that says "You have no messages"...
[[messageTable tableFooterView] removeFromSuperview];
[messageTable beginUpdates];
[messageTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
[messageTable endUpdates];
[self scrollToNewestMessage];
}
}
當我收到遠程通知和它火時調用此方法。日誌語句打印。但是,UITableView不會更新。對我來說特別奇怪的是,如果我在IBAction中放入相同的代碼並將其鏈接到故事板中的刷新按鈕,它就會起作用。有沒有什麼特別的,我必須做的以編程方式更新UITableView?
在調用此更新調用之前,請確保您的數據源已經更新了此新行的數據。我假設你知道在表格中動畫行並向數據源添加行是離散操作。 – 2012-04-13 23:05:59
是的,謝謝。如果我在進行insertRowsAtIndexPaths:withAnimation調用之前記錄了我用於填充表的數組的計數,我會得到預期的更新計數。這部分似乎很好。另外,如果我的數據源在我嘗試插入一個新行時還沒有更新,我會得到一個NSException並崩潰,難道我不會? – geraldWilliam 2012-04-13 23:14:57
好點,那會拋出一個'NSInternalInconsistencyException'。如果您在表格中的位置比添加的行更靠後,它會滾動到正確的行嗎?另外,我不確定是否將tableFooterView從其超級視圖中移除是一個好主意,因爲這是UITableView的視圖層次結構的一部分。我可能會將其設置爲「nil」。這似乎很奇怪,這個工程時,按下了一個按鈕... – 2012-04-13 23:18:32