objective-ccocoansscrollviewnscollectionview

NSCollectionView How to scroll to selected item


I programmatically select a item in my NSCollectionView. The item is selected as expected but the view doesn't scroll automatically to this item.

[collectionView setSelectionIndexes:[NSIndexSet indexSetWithIndex:compt]];

How to scroll to the selected item ?


Solution

  • Find the answer here NSScrollView: Make sure frame is visible

    This code worked for me :

    NSRect selectionRect = [self.collectionView frameForItemAtIndex:[[self.collectionView selectionIndexes] firstIndex]];
    [self.collectionView scrollRectToVisible:selectionRect];