Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSPagerView inside UITableView : need to set pagerView currentIndex before it gets displayed #54

Closed
lucasgerard opened this issue Jun 17, 2017 · 3 comments

Comments

@lucasgerard
Copy link

Hi,

I'm currently using FSPagerView inside a UITableView, just as it was a CollectionView from this article

However, I can't find a way to set the contentOffset of the pagerView as I would for a collectionView (collectionView.contentOffset.x = newValue), to ensure that when my tableView cells are reused, the offset are preserved and my pagerView is displayed with the right currentIndex.

Basically, I would like to set the pagerView current displayed index in my tableView WillDisplayCell method, but as I try using scrollToItem it crashes with an index out of range issue showing that numberOfItems is still 0, as its data was not loaded yet.

Any idea how to accomplish that? Hope I'm clear enough, thanks for your help!

@WenchaoD
Copy link
Owner

The content offset is useless if your collectionView hasn't layout it's content. Typically you should set the content offset in viewDidLayoutSubviews function of your view controller, or force the collection view layout before scrolling by calling layoutIfNeeded(). In this case, the following code could be helpful.

pagerView.layoutIfNeeded()
pagerView.scrollToItem(at: index, animated: false)

@lucasgerard
Copy link
Author

Thanks for the help, it worked!

@WenchaoD
Copy link
Owner

You are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants