我有一個UITableView,我正在用動畫添加一行(使用insertRowsAtIndexPaths:withRowAnimation:
)。只要桌子不長於屏幕,這一切都很好。UITableView滾動到底部看到插入動畫
如果它比屏幕大,那麼我想滾動到底部,但它不是我想要的。如果在添加後滾動到新行,我會錯過動畫。如果我試圖在添加之前滾動到indexPath,它會拋出一個異常(關於它不是一個有效的indexPath)
是否有解決方案,而不是添加一個空行?
我有一個UITableView,我正在用動畫添加一行(使用insertRowsAtIndexPaths:withRowAnimation:
)。只要桌子不長於屏幕,這一切都很好。UITableView滾動到底部看到插入動畫
如果它比屏幕大,那麼我想滾動到底部,但它不是我想要的。如果在添加後滾動到新行,我會錯過動畫。如果我試圖在添加之前滾動到indexPath,它會拋出一個異常(關於它不是一個有效的indexPath)
是否有解決方案,而不是添加一個空行?
是的,有沒有增加一個空白行的解決方案。
注:在代碼中,我認爲只有1個部分,但有很多行。您也可以修改代碼以管理多個部分。
- (void)theMethodInWhichYouInsertARowInTheTableView
{
//Add the object in the array which feeds the tableview
NSString *newStringObject = @"New Object";
[arrayWhichFeeds addObject:newStringObject];
[myTableView beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1) inSection:0]];
[myTableView insertRowsAtIndexPaths:paths withRowAnimation:NO];
[myTableView endUpdates];
[myTableView reloadData];
[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
記住在主線程中這樣做,否則它不會滾動到所需的位置。
我有這個相同的問題。這是我的解決方案。
首先 - 更新您的數據源
二 -
NSIndexPath *path = [NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1 inSection:0]];
NSArray *paths = [NSArray arrayWithObject:path];
[myTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[myTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];
*請注意,這不是包裹在[tableView beginUpdades]
和[tableView endUpdates]
代碼。如果你這樣做,它不會按需要工作。
試試看,它應該在滾動到它們的同時在底部的新行中設置動畫。
更一般地,滾動到下:
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([self.table numberOfRowsInSection:([self.table numberOfSections] - 1)] - 1) inSection:([self.table numberOfSections] - 1)];
[self.table scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
其他技術,包括在這個問題對我來說沒有工作提到的那些。但是,這確實如此:
當我第一次開始iOS開發時,我開發了這個技術(並寫下了這個答案),但是這確實長期有效。
這適用於我。不是插入一行,而是突出顯示它(通過淡入淡出)。但原理是相同的。
if let definiteIndexPath = indexPathDelegate.getIndexPath(toDoItem) {
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.tableView.scrollToRowAtIndexPath(definiteIndexPath, atScrollPosition: .Middle, animated: false)
}, completion: {
(finished: Bool) -> Void in
// Once the scrolling is done fade the text out and back in.
UIView.animateWithDuration(5, delay: 1, options: .Repeat | .Autoreverse, animations: {
self.tableView.reloadRowsAtIndexPaths([definiteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}, completion: nil)
})
}
實施例:
YOUR_ARRAY.append("new string")
tableView.insertRows(at: [IndexPath(row: YOUR_ARRAY.count-1, section: 0)], with: .automatic)
tableView.scrollToRow(at: IndexPath(row: YOUR_ARRAY.count-1, section: 0), at: UITableViewScrollPosition.bottom, animated: true)
的'-insertRowsAtIndexPaths第二個參數:withRowAnimation:'不是'BOOL';它是一個'UITableViewRowAnimation'類型 – user102008 2011-04-27 23:37:34
我仍然沒有看到用這種技術添加動畫。它滾動得很好,但即使我爲withRowAnimation(不是「NO」,但是UITableViewRowAnimationFade)傳遞一個有效值,我也從來沒有看到它發生。這個問題(我認爲)是如何滾動,仍然看到添加動畫(對吧?) – Aardvark 2013-02-01 17:07:15
它工作得很好,如果你使用'UITableViewRowAnimation.Bottom' – zuziaka 2015-11-30 15:33:57