简体   繁体   English

使用UICollectionView scrollToItemAtIndexPath返回到上一个选定的单元格

[英]Using UICollectionView scrollToItemAtIndexPath to return to last selected cell

I have UICollectionView for the user to select images from a grid. 我有UICollectionView供用户从网格中选择图像。 Once a cell is selected, the view controller is released, including the UICollectionView. 选定单元格后,将释放视图控制器,包括UICollectionView。

I'd like to remember where the user was last time they used the UICollectionView and automatically scroll to that location when the view controller is loaded again. 我想记住用户上一次使用UICollectionView的位置,并在再次加载视图控制器时自动滚动到该位置。

The problem I'm encountering is when this can be done. 我遇到的问题是何时可以完成。 I'm assuming I need to wait until the UICollectionView has been fully loaded before executing scrollToItemAtIndexPath:atScrollPosition:animated: . 我假设我需要等到UICollectionView完全加载后再执行scrollToItemAtIndexPath:atScrollPosition:animated:

What's the preferred way to determine when the view controller and UICollectionView are fully laid out? 确定何时完全布局视图控制器和UICollectionView的首选方法是什么?

I spent some time exploring solutions. 我花了一些时间探索解决方案。 @Leo, I was not able to scroll in viewDidLoad . @Leo,我无法在viewDidLoad滚动。 However, I was able to achieve good results keeping track of the state of the view life cycle. 但是,在跟踪视图生命周期的状态时,我能够取得良好的结果。

I created a constant to remember the content offset. 我创建了一个常数来记住内容偏移量。 I used a constant so it would retain it's value between loads of the VC. 我使用了一个常量,因此它将在VC的加载之间保留其值。 I also created a property to flag when it was okay to scroll: 我还创建了一个属性来标记何时可以滚动:

static CGPoint kLastContentOffset;
@property (nonatomic) BOOL autoScroll;
@property (weak, nonatomic) IBOutlet UICollectionView *collection;

The life cycle code I used: 我使用的生命周期代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.autoScroll = NO; // before CollectionView laid out
}

- (void)viewDidDisappear:(BOOL)animated
{
    kLastContentOffset = self.collection.contentOffset;
    [super viewDidDisappear:animated];
}

- (void)viewDidLayoutSubviews
{
    if (self.autoScroll) { // after CollectionView laid out
        self.autoScroll = NO; // don't autoScroll this instantiation of the VC again
    if (kLastContentOffset.y) {
        [self.collection setContentOffset:kLastContentOffset];
    }
}

During the layout of the collectionView I set the flag indicating that the next viewDidLayoutSubviews should auto scroll: 在collectionView的布局期间,我设置了标志,指示下一个viewDidLayoutSubviews应该自动滚动:

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    self.autoScroll = YES;
    // return count here
}

The important part was saving the content offset when my view disappeared. 重要的部分是当我的视图消失时保存内容偏移。 The constant is not remembered between launches of the application which what I wanted and the reason for not saving it in preferences. 在启动我想要的应用程序和不将其保存在首选项中的原因之间,没有记住该常数。

Seems like there has to be a more elegant solution but this works well. 似乎必须有一个更优雅的解决方案,但这很好。

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

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