我正在處理的應用程序的一部分涉及一個消息接口,我嘗試使其類似於iOS消息應用程序的接口。當收到或發送新郵件時,我希望表格視圖向下滾動並顯示插入動畫。隨着新行的添加,保持UITableView滾動到底部
到目前爲止,我已經基本上做到了這一點:
- (void)didReceiveNewMessage:(...) {
[messages addObject:...]; // add the new message to an array
NSIndexPath *newIndexPath = [NSIndexPath ...]; // find out where the new cell goes
[tableView insertRowAtIndexPath:newIndexPath ...]; // animate the insertion of the new message
[tableView scrollToRowAtIndexPath:newIndexPath ...]; // do the scrolling
}
這個工作,如果消息被慢慢英寸但是,如果我真的很快就給自己發送了一堆消息,我注意到表格視圖會「變得混亂」。
例如,如果我快速發送消息1,2,3,4,5,6,表格視圖將動畫插入1,2,3和4,然後停止滾動。如果我實際上手動滾動表格視圖,我可以看到下面的5和6。任何人都知道更好的方法來做到這一點?
我曾嘗試setContentOffset:
來滾動表視圖,但是這有同樣的效果。
你是否異步調用此方法? – Iducool 2012-07-16 08:32:08
嘗試調用scrollToRowAtIndexPath fist,然後insertRowAtIndexPath。 – 2012-07-16 08:37:19
如果您發現這是同步問題,請添加[tableView peformSelectorOnMainThread:@selector(reloadData)];但是,我不確定其重新裝載數據實際上會分別回滾到位置1還是位置0。 – 2012-07-16 08:40:49