OGeek|极客世界-中国程序员成长平台

标题: ios - 如何实时获取collectionView当前节号 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:01
标题: ios - 如何实时获取collectionView当前节号

- (void)scrollViewDidScrollUIScrollView *)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];
}

控制台一直显示“sectionNumber===0”,但实际上应该是1到10。我不知道我哪里出错了,而且我设置了collectionView的delegate。 这已经浪费了我 3 个小时了,我真的需要一些帮助。



Best Answer-推荐答案


经过半天的努力,我终于解决了这个问题。解决这个问题的关键是CollectionView的两个delegate函数:

- (void)collectionViewUICollectionView *)collectionView willDisplaySupplementaryViewUICollectionReusableView *)view forElementKindNSString *)elementKind atIndexPathNSIndexPath *)indexPath

- (void)collectionViewUICollectionView *)collectionView didEndDisplayingSupplementaryViewnonnull UICollectionReusableView *)view forElementOfKindnonnull NSString *)elementKind atIndexPathnonnull NSIndexPath *)indexPath

通过这两个委托(delegate)函数,您可以正确获取当前部分。

关于ios - 如何实时获取collectionView当前节号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482086/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4