UITableView scroll to bottom to see insertion animation
我有一个UITableView,我正在向其中添加一个动画行(使用
如果它大于屏幕,那么我试图滚动到底部,但是我想要的不是很有效。如果添加后滚动到新行,则会错过动画。如果在添加索引路径之前尝试滚动到该索引路径,它将引发异常(关于它不是有效的索引路径)
除了添加空白行之外,还有其他解决方法吗?
我遇到了同样的问题。这是我的解决方法。
第一-
更新您的数据源
第二个-
1 2 3 4 | 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]; |
*请注意,此代码未package在
尝试一下,它应该在滚动到新行时从底部开始为新行设置动画。
是的,没有添加空白行的解决方案。
注意:在代码中,我认为只有1个部分,但有很多行。您也可以修改代码以管理多个部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (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]; } |
其他技术(包括在此问题下提及的技术)对我不起作用。但是,这样做:
当我第一次开始iOS开发时,我就解决了这种技术(并写下了这个答案),但是长期来看确实可行。
请务必在主线程中执行此操作,否则它将不会滚动到所需位置。
示例:
1 2 3 | 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) |
这对我有用。我没有插入行,而只是突出显示(通过淡入淡出)。但是原理是相同的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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) }) } |
通常,滚动到底部:
1 2 | 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]; |