2016-09-14 8 views
2
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    NSIndexPath *path = [self.collectionView indexPathForItemAtPoint:CGPointMake(self.collectionView.contentOffset.x, self.collectionView.contentOffset.y)]; 
    NSLog(@"sectionNumber===%ld",path.section); 

// 菜单button滚动到相应的位置 
    [self.menuButtonView scrollToButtonWithButtonNumber:path.section]; 
} 

リアルタイムでcollectionViewの現在のセクション番号を取得することができ、さらに、私はcollectionViewのデリゲートを設定しました。 もう3時間も無駄です。本当に助けが必要です。は、どのように私はコンソールが「区間番号=== 0」すべての時間を示しているが、実際に、それは私がミスをしたところ、私は知らない1〜10でなければなりません

+1

1) 'contentOffset'は既に' CGPoint'ですので、 'indexPathForItemAtPoint'に直接渡すことができます。 2)実際に 'contentOffset'に項目があることを確認しましたか?多分それはマージンのポイントです。 – Avi

+0

複数のセクションがあるか、path.rowを探していますか? –

答えて

2

、私はこの問題を解決するためにlast.Theキーでこの問題を解決しCollectionViewの2つのデリゲート関数です:

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(nonnull UICollectionReusableView *)view forElementOfKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath 

この2つのデリゲート機能を使用すると、現在のセクションを正しく取得できます。

1

scrollViewDidScrollでは、コレクションビューの表示セルのすべてのインデックスパスを取得し、中間インデックスパスを取得してそのセクションを取得できます。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    NSArray *indexPaths = [self.mainImageCollection indexPathsForVisibleItems]; 
    NSIndexPath *middleIndexPath = [indexPaths objectAtIndex:([indexPaths count]/2)];//you must check if indexPaths >=2 or <2 
    NSLog(@"indexPath : %d at Section :%d",middleIndexPath.item,middleIndexPath.section); 
} 

このヘルプが欲しいです!

+0

助けていただきありがとうございます。しかし、うまくいきませんでした。 –

+1

雲あなたはもっと説明してください。 – user3335651

0

希望日の努力の半分の後にこのヘルプあなた

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    NSIndexPath *centerCellIndexPath = [self.collectionView indexPathForItemAtPoint:[self.view convertPoint:[self.view center] toView:self.collectionView]]; 
    NSLog(@"Index:%ld",(long)centerCellIndexPath.row);  
} 
+0

ありがとうございましたが、それは私のためには機能しませんでした。 –

関連する問題

 関連する問題