简体   繁体   English

UICollectionView scrollToItemAtIndexPath:并获取新的单元格位置

[英]UICollectionView scrollToItemAtIndexPath: and get fresh cell position

I would like to scroll collection view to some offscreen cell without animation and get position of that cell after scrolling. 我想将集合视图滚动到一些没有动画的屏幕外单元格,并在滚动后获取该单元格的位置。 The problem is that collection view scrolls correctly (visually) but cell frame remains offscreen. 问题是集合视图正确(可视)滚动但单元格框架仍然在屏幕外。 Here is my code: 这是我的代码:

NSIndexPath *path = [fetchedResultsController indexPathForObject:objectInCollection];
[collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
[collectionView layoutSubviews]; // or 'layoutIfNeeded', doesn't matter
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:path];
cellFrame = [cell.superview convertRect:cell.frame toView:nil]; // still not refreshed

I can propose that collection view applies scrolling not immediately, so I would like to find approach to apply the scrolling. 我可以建议集合视图不立即应用滚动,所以我想找到应用滚动的方法。

--- UPDATE --- ---更新---

The collection view is in previous view controller (so I scroll it from code and then pop visible view controller). 集合视图位于以前的视图控制器中(因此我从代码中滚动它,然后弹出可见的视图控制器)。

Cell frame remains offscreen even in viewDidAppear: method of previous view controller. 即使在viewDidAppear:前一个视图控制器的方法,单元格框架仍然在屏幕外。 To be exact, [cell.superview convertRect:cell.frame toView:nil]; 确切地说, [cell.superview convertRect:cell.frame toView:nil]; returns frame without contentOffset of collection view. 返回框而不contentOffset集合视图的。 So CGRectOffset([cell.superview convertRect:cell.frame toView:nil], 0, -collectionView.contentOffset.y) returns correct cell frame on screen. 所以CGRectOffset([cell.superview convertRect:cell.frame toView:nil], 0, -collectionView.contentOffset.y)在屏幕上返回正确的单元格框架。

As i can understand from your problem statement, you want to set the some cell which is out of the view to visible state. 正如我从您的问题陈述中可以理解的那样,您希望将视图外的某个单元格设置为可见状态。

The cells are placed at the same position when the UICollectionView calculates 当UICollectionView计算时,单元格放置在相同的位置

  • (void)prepareLayout (空隙)prepareLayout
  • (void)layoutAttributes.. methods. (void)layoutAttributes ..方法。

The frames of the collection cells are fixed till the invalidateLayout or reloaddata is called. 收集单元的帧是固定的,直到调用invalidateLayout或reloaddata。 Just you have to work around with the contentOffset of the collectionview. 只需要处理集合视图的contentOffset。 Just get the frame of cell and set the contentOffset so the your frame of cell is visible on the screen. 只需获取单元格框架并设置contentOffset,即可在屏幕上看到您的单元格框架。 Hope it helps. 希望能帮助到你。

The cell frame should be correct and I don't think you need to use convertRect at all. 单元格框架应该是正确的,我认为你根本不需要使用convertRect。

cellFrame = cell.frame; cellFrame = cell.frame;

However, you probably have to do this after the collection view loads. 但是,您可能必须在加载集合视图后执行此操作。 Call [collectionView reloadData] first. 首先调用[collectionView reloadData]。

There is also a layoutAttributesForItemAtIndexPath: method that has the attributes for that cell. 还有一个layoutAttributesForItemAtIndexPath:方法,该方法具有该单元格的属性。 You can call this on the indexPath and get the attributes that way. 您可以在indexPath上调用它并以这种方式获取属性。

尝试将滚动位置更改为UICollectionViewScrollPositionBottom ,这对我有用 -

[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];

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

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