Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动到特定集合视图标题(非单元格)_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios 滚动到特定集合视图标题(非单元格)

Ios 滚动到特定集合视图标题(非单元格),ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有一个带有索引视图的collectionview自定义视图,因为apple one仅适用于UITableView,该索引视图是一个UIControl子类,在触发值更改事件时调用方法。 显然,被调用的方法将集合视图滚动到与被触摸字母相关联的部分。 下面是我对该方法的实现: - (IBAction)indexViewValueChanged:(id)sender { NSIndexPath * indexPath = [NSIndexPath indexPathForItem:0 inSe

我有一个带有索引视图的collectionview自定义视图,因为apple one仅适用于UITableView,该索引视图是一个UIControl子类,在触发值更改事件时调用方法。 显然,被调用的方法将集合视图滚动到与被触摸字母相关联的部分。 下面是我对该方法的实现:

- (IBAction)indexViewValueChanged:(id)sender
{
    NSIndexPath * indexPath = [NSIndexPath indexPathForItem:0 inSection: [sender currentIndex]];

    [collectionView scrollToItemAtIndexPath: indexPath atScrollPosition: UICollectionViewScrollPositionTop animated: NO];

    collectionView.contentOffset = CGPointMake(collectionView.contentOffset.x, MAX(collectionView.contentOffset.y - [(UICollectionViewFlowLayout *)[collectionView collectionViewLayout] headerReferenceSize].height, 0));
}
问题是,由于最后一行的contentOffset调整,某些情况不起作用。 例如,如果相关部分下面的内容小于collectionView,则标题不会位于scrollToItemAtIndexPath:atScrollPosition:animated:call之后的屏幕顶部,这是正常的,但我希望防止在下一行代码中进行contentOffset调整。 我的问题是,我不知道如何确定标题已经可见并避免这种contentOffset调整


我有任何想法:

因为每个节只能有一个标题,所以滚动到第一个项,其nsindepath项值为0,节值为目标节的编号

有几种方法可以搜索这些特定的索引路径值。这里有一个方法:

[self.collectionView.visibleCells indexOfObjectPassingTest:^BOOL(__kindof UICollectionViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BOOL result = ([self.collectionView indexPathForCell:obj].item == 0 && [self.collectionView indexPathForCell:obj].section == 2);
            // Scroll to item at [self.collectionView indexPathForCell:obj]
        stop = &result;
        return result;
    }];

UICollectionView有一个属性visibleCells数组,其中包含所有当前可见的单元格。我只想滚动到节标题上,而不是单元格上可能重复的Mikael,你是对的,我对垂直集合视图也有同样的问题,但在这种情况下,这并不重要。谢谢你给我指出这篇文章。