32

CollectionViewコントロールを作成し、画像で埋めました。ここで、開始時に特定のインデックスの項目にスクロールしたいと考えています。私は次のように試しましたscrollToItemAtIndexPath

[self.myFullScreenCollectionView scrollToItemAtIndexPath:indexPath 
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

ただし、次の例外が発生しています。どこが間違っているのか、誰かが私を案内してくれませんか。

2013-02-20 02:32:45.219 ControlViewCollection1[1727:c07] *** Assertion failure in 
-[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17
/UICollectionViewData.m:485 2013-02-20 02:32:45.221 ControlViewCollection1[1727:c07] must return a 
UICollectionViewLayoutAttributes instance from -layoutAttributesForItemAtIndexPath: for path 
<NSIndexPath 0x800abe0> 2 indexes [0, 4]
4

9 に答える 9

53

ビュー コントローラの読み込み中にスクロールしようとしている場合は、 を呼び出す前に を必ず呼び出しlayoutIfNeededてください。これは、親ビューのサブビューがレイアウトされるたびにスクロール操作を実行しないため、viewDidLayoutSubviews にスクロール ロジックを配置するよりも優れています。UICollectionViewscrollToItemAtIndexPath

于 2015-02-26T20:23:40.487 に答える
7

Uはこれを行うことができ、viewDidLoadメソッドで

preformBatchUpdates を呼び出すだけです

[self performBatchUpdates:^{
        if ([self.segmentedDelegate respondsToSelector:@selector(segmentedBar:selectedIndex:)]){
            [self.segmentedDelegate segmentedBar:self selectedIndex:_selectedPage];
        }
    } completion:^(BOOL finished) {
        if (finished){
            [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedPage inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }

    }];

私の場合、サブクラス CollectionView にはプロパティ selectedPage があり、このプロパティのセッターで呼び出します

- (void)setSelectedPage:(NSInteger)selectedPage {
    _selectedPage = selectedPage;
    [self performBatchUpdates:^{
        if ([self.segmentedDelegate respondsToSelector:@selector(segmentedBar:selectedIndex:)]){
            [self.segmentedDelegate segmentedBar:self selectedIndex:_selectedPage];
        }
    } completion:^(BOOL finished) {
        if (finished){
            [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedPage inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }

    }];
}

ビューコントローラーでは、これをコードで呼び出します

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.segmentedPages.selectedPage = 1;
}
于 2014-04-07T11:58:19.107 に答える
5

また、適切な UICollectionviewScrollPosition 値を使用する必要があることも覚えておいてください。明確にするために、以下のコードを参照してください。

typedef NS_OPTIONS(NSUInteger, UICollectionViewScrollPosition) {
UICollectionViewScrollPositionNone                 = 0,

/* For a view with vertical scrolling */
// The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
// Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
UICollectionViewScrollPositionTop                  = 1 << 0,
UICollectionViewScrollPositionCenteredVertically   = 1 << 1,
UICollectionViewScrollPositionBottom               = 1 << 2,

/* For a view with horizontal scrolling */
// Likewise, the horizontal positions are mutually exclusive to each other.
UICollectionViewScrollPositionLeft                 = 1 << 3,
UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
UICollectionViewScrollPositionRight                = 1 << 5

};

于 2013-08-14T09:01:57.710 に答える
3

スクロールロジックを追加するとviewDidAppearうまくいきました:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.collectionView?.scrollToItemAtIndexPath(
        someIndexPath,
        atScrollPosition: UICollectionViewScrollPosition.None,
        animated: animated)
}

に追加してもviewDidLoad機能しません。無視されます。

viewDidLayoutSubviews何かが変更されたときにいつでも呼び出すロジックをスクロールしたくない限り、それを追加しても機能しません。私の場合、ユーザーがアイテムを手動でスクロールできなくなりました

于 2016-04-04T20:49:35.033 に答える