简体   繁体   中英

Connecting uipagecontrol to my whole uicollectionview, not to the individual cells

I have a UICollectionView (in a UIViewController) displaying 20 cells/images per page. I have set the UICollectionView's scroll direction to Horizontal and layout to Flow . I've enabled Scrolling and Paging . So when i have more then 20 cells/images i can swipe horizontally to page to the next lot of cells/images. This works perfectly!

However , what i would like to do now is add a way to show how many pages are available and which one i'm currently on. I assumed UIPageControl is the way to go, so I've placed it nicely underneath my UICollectionView.

The Question is : How do i connect the UIPageControl to my existing scrolling / paging UICollectionView ?

Look forward to your responses.

The collection view (since it's a sort of scroll view) allows the vc to be a scroll view delegate. Set the delegate to your vc, and implement scrollViewDidScroll . Therein, compute the page by asking the collection view for indexPathsOfVisibleCells . With any index path in the returned array, you can compute the current page with indexPath.row / 20 .

Page control lets you set currentPage and numberOfPages as properties. If I understand your setup correctly, numberOfPages == the datasource array count / 20.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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