ios 记住 UITableView 上的滚动位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18843574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remembering scroll position on UITableView
提问by mylogon
I have a bit of a problem with my iOS app in xcode. I have a UITableView that loads a few hundred cells. When I scroll down to a specific cell and drill down to detailedviewcontrollers and return again the master view table has returned all the way to the top. I've had a look at the following 2 questions that are similar.
我在 xcode 中的 iOS 应用程序有一些问题。我有一个加载几百个单元格的 UITableView。当我向下滚动到特定单元格并向下钻取到 detailviewcontrollers 并再次返回时,主视图表已返回到顶部。我看过以下两个相似的问题。
How can I get the UITableView scroll position so I can save it?
Setting scroll position in UITableView
I still can't get these to work. I'm not the most experienced coder so I'm really struggling with this.
我仍然无法让这些工作。我不是最有经验的编码员,所以我真的很挣扎。
I know things like viewWillDisappear and viewDidAppear need to be changed, but I just really can't get much further than that.
我知道 viewWillDisappear 和 viewDidAppear 之类的东西需要改变,但我真的不能比这更进一步了。
On this table I have a reloadData feature so it is possible to pull down the latest data from the server and also a working search bar.
在这张表上,我有一个 reloadData 功能,因此可以从服务器和一个有效的搜索栏下拉最新数据。
Anyway, a helping hand would be great. Thanks,
无论如何,伸出援助之手就太好了。谢谢,
Luke
卢克
采纳答案by mylogon
Sorted!
排序!
With a bit of inspiration from Desdenova, whilst at work I had a good think about it and realised what it could be. Remembering that I had a search bar, I had implemented the following code a few months ago to hide it:
在 Desdenova 的启发下,我在工作中好好思考并意识到它可能是什么。记得我有一个搜索栏,几个月前我实现了以下代码来隐藏它:
[self.tableView setContentOffset:CGPointMake(0,-20) animated:NO];
In naivety I put that in viewDidAppear
rather than viewDidLoad
. Obviously, this did hide the search bar, but with it being in the wrong void command it did it every time the masterDetailView returned to the top of the stack. So, after moving the above code to viewDidLoad it now still hides it, but only the once.
我天真地把它放在viewDidAppear
而不是viewDidLoad
. 显然,这确实隐藏了搜索栏,但是由于它在错误的 void 命令中,每次 masterDetailView 返回到堆栈顶部时它都会这样做。所以,在将上面的代码移动到 viewDidLoad 之后,它现在仍然隐藏它,但只有一次。
I'm just spelling it out like this for other beginners, like myself, who may come across the same problem and may just save their sanity!
我只是为其他初学者(例如我自己)这样拼写,他们可能会遇到同样的问题并且可能只是为了挽救他们的理智!
Thanks to you all for your ideas that helped me out.
感谢大家的想法帮助我。
+1 to you all!
+1给你们所有人!
回答by Prashant Nikam
See here you have to first save the scrolled position of tableView
i.e. contentOffset
of tableView
and then reuse it when you are coming back to tableView
.
看到这里你必须先保存tableView
ie contentOffset
of的滚动位置,tableView
然后在你回到tableView
.
1)When and where you will save it :
1)何时何地保存它:
When: At the point, when you are drilling down to detailViewController
save the contentOffset
of tableView
当:在点,当你向下钻取到detailViewController
保存contentOffset
的tableView
How: float verticalContentOffset = tableView.contentOffset.y;
如何:float verticalContentOffset = tableView.contentOffset.y;
2) How will you set tableView back to the same scrolled position :
2) 您将如何将 tableView 设置回相同的滚动位置:
[tableView setContentOffset:CGPointMake(0, verticalContentOffset)];
Hope this will help you.
希望这会帮助你。
回答by Enrico Susatyo
If anyone is wondering, I tried the solution by Prashant N and it worked. However reloadData
don't actually reset the scroll position anymore now, so I didn't have to do anything other than reloadData
.
如果有人想知道,我尝试了 Prashant N 的解决方案,并且奏效了。但是,reloadData
现在不再实际重置滚动位置,因此除了reloadData
.
回答by Guilherme Carvalho
My solution to this problem consisted in scrolling the table to the indexrow of the last item.
我对这个问题的解决方案包括将表格滚动到最后一项的索引行。
private func scrollToItem(item:Int, section:Int bottom:Bool = true, animated:Bool = false) {
let indexPath = NSIndexPath(forRow: item:Int-1, inSection: section)
var position = UITableViewScrollPosition.Bottom
if (!bottom) { position = UITableViewScrollPosition.Top }
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: position, animated: animated)
}
回答by vib
For some reason this was halfway working for me (sometimes the position would only be approximate). I finally used the following a priori less clean solution
出于某种原因,这对我来说是半途而废(有时这个位置只是近似的)。我终于使用了以下先验不太干净的解决方案
NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0];
[self.tableView scrollToRowAtIndexPath:self.firstVisibleIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
self.firstVisibleIndexPath = firstVisibleIndexPath;
which worked better.
哪个效果更好。
回答by andyzei
As mentioned in other comments, scrolling to the top on Back navigation in a UINavigationController is not the default behavior.
正如其他评论中提到的,在 UINavigationController 中的后退导航上滚动到顶部不是默认行为。
Another cause might be that you are setting a cell near the top of the table as first responder in viewWillAppear, e.g. a search box or edit field near the top of your table. UITableViewController will scroll a first responder into view automatically, which is nice. As long as that's what you're trying to do. :) Take care to not set first responder in viewWillAppear unless that's what you want. Cheers, AZ
另一个原因可能是您将表格顶部附近的单元格设置为 viewWillAppear 中的第一响应者,例如表格顶部附近的搜索框或编辑字段。UITableViewController 会自动将第一响应者滚动到视图中,这很好。只要这就是你想要做的。:) 注意不要在 viewWillAppear 中设置第一响应者,除非这是你想要的。干杯,亚利桑那州
回答by Dmitry Malysh
My decision for a collection:
我的收藏决定:
CGFloat oldCollectionViewHeight = self.messageCollectionView.contentSize.height;
[self.messageCollectionView reloadData];
[self.messageCollectionView layoutIfNeeded];
CGFloat newCollectionViewHeight = self.messageCollectionView.contentSize.height;
if (oldCollectionViewHeight) {
self.messageCollectionView.contentOffset = CGPointMake(0, newCollectionViewHeight - oldCollectionViewHeight);
}
For the table, the principle is the same.
对于表,原理是一样的。
回答by aph340
You don't need to do anything, while you are in the same view controller, your position will be the same. I'm afraid that somewhere in your view controller is being called a reloadData. Search for some method calling scrollToRowAtIndexPath.
你不需要做任何事情,当你在同一个视图控制器中时,你的位置将是相同的。恐怕您的视图控制器中的某个地方被称为 reloadData。搜索一些调用 scrollToRowAtIndexPath 的方法。