Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: current index path for page control

I'm using a UICollectionView with a flow layout to show a list of cells, I also have a page control to indicate current page, but there seems to be no way to get current index path, I know I can get visible cells:

UICollectionView current visible cell index

however there can be more than one visible cells, even if each of my cells occupies full width of the screen, if I scroll it to have two halves of two cells, then they are both visible, so is there a way to get only one current visible cell's index?

Thanks

like image 327
hzxu Avatar asked Dec 17 '13 05:12

hzxu


People also ask

How do I get the indexPath of a cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is the difference between Uitableview and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .

What is UICollectionView flow layout?

Overview. A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.


1 Answers

You can get the current index by monitoring contentOffset in scrollViewDidScroll delegate

it will be something like this

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {     NSInteger currentIndex = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;  } 
like image 139
andykkt Avatar answered Sep 28 '22 01:09

andykkt