简体   繁体   English

记住UITableView上的滚动位置

[英]Remembering scroll position on UITableView

I have a bit of a problem with my iOS app in xcode. 我在xcode中的iOS应用存在问题。 I have a UITableView that loads a few hundred cells. 我有一个UITableView可以加载数百个单元格。 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. 当我向下滚动到一个特定的单元格并向下钻取到详细的viewcontrollers并再次返回时,主视图表一直返回到顶部。 I've had a look at the following 2 questions that are similar. 我看了以下两个相似的问题。

How can I get the UITableView scroll position so I can save it? 如何获取UITableView滚动位置以便保存它?

Setting scroll position in UITableView 在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 路加

See here you have to first save the scrolled position of tableView ie contentOffset of tableView and then reuse it when you are coming back to tableView . 看到这里,你必须先保存的滚动位置tableViewcontentOffsettableView然后再用它,当你回来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保存tableViewcontentOffset

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. 希望这会帮助你。

Sorted! 排序!

With a bit of inspiration from Desdenova, whilst at work I had a good think about it and realised what it could be. 在工作中,我从Desdenova身上获得了一些启发,对此我进行了认真的思考,并意识到了它的可能。 Remembering that I had a search bar, I had implemented the following code a few months ago to hide it: 记住我有一个搜索栏,几个月前我实现了以下代码以将其隐藏:

[self.tableView setContentOffset:CGPointMake(0,-20) animated:NO];

In naivety I put that in viewDidAppear rather than viewDidLoad . 出于天真,我将其放在viewDidAppear而不是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. 显然,这确实隐藏了搜索栏,但是由于它位于错误的void命令中,因此每次masterDetailView返回堆栈顶部时,它都会进行搜索。 So, after moving the above code to viewDidLoad it now still hides it, but only the once. 因此,将上面的代码移动到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!

If anyone is wondering, I tried the solution by Prashant N and it worked. 如果有人想知道,我尝试了Prashant N的解决方案,它奏效了。 However reloadData don't actually reset the scroll position anymore now, so I didn't have to do anything other than reloadData . 但是reloadData现在实际上不再重置滚动位置了,因此除了reloadData之外,我无需执行其他任何操作。

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)
}

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. 效果更好。

As mentioned in other comments, scrolling to the top on Back navigation in a UINavigationController is not the default behavior. 如其他注释中所述,滚动到UINavigationController中的Back导航顶部不是默认行为。

Another cause might be that you are setting a cell near the top of the table as first responder in viewWillAppear, eg a search box or edit field near the top of your table. 另一个原因可能是您正在将表格顶部附近的单元格设置为viewWillAppear中的第一响应者,例如,表格顶部附近的搜索框或编辑字段。 UITableViewController will scroll a first responder into view automatically, which is nice. UITableViewController会自动将第一个响应者滚动到视图中,这很好。 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. :)注意不要在viewWillAppear中设置第一响应者,除非您要这样做。 Cheers, AZ 亚利桑那州,干杯

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. 对于表,原理是相同的。

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. 恐怕您的视图控制器中的某处被称为reloadData。 Search for some method calling scrollToRowAtIndexPath. 搜索一些调用scrollToRowAtIndexPath的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM