NSCollectionView如何滚动到选定项

时间:2023-01-19 10:03:55

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

我以编程方式在NSCollectionView中选择一个项。项目按预期选择,但视图不会自动滚动到该项目。

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

How to scroll to the selected item ?

如何滚动到选定的项目?

3 个解决方案

#1


6  

Find the answer here NSScrollView: Make sure frame is visible

在这里找到答案NSScrollView:确保框架是可见的。

This code worked for me :

这段代码对我有用:

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

#2


2  

Since OS X 10.11 in the new NSCollectionView you should use instead:

因为在新的NSCollectionView中,OS X 10.11应该使用以下内容:

[self.collectionView scrollToItemsAtIndexPaths:[self.collectionView selectionIndexPaths] scrollPosition:NSCollectionViewScrollPositionCenteredHorizontally];

#3


0  

let scrollPositionX: CGFloat = videoThumbCollectionView.visibleRect.origin.x + 150.0 videoThumbCollectionView.scroll(NSPoint(x: scrollPositionX, y: videoThumbCollectionView.visibleRect.origin.y))

让scrollPositionX: CGFloat = videoThumbCollectionView.visibleRect.origin。videoThumbCollectionView x + 150.0。滚动(NSPoint(x:scrollPositionX y:videoThumbCollectionView.visibleRect.origin.y))

#1


6  

Find the answer here NSScrollView: Make sure frame is visible

在这里找到答案NSScrollView:确保框架是可见的。

This code worked for me :

这段代码对我有用:

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

#2


2  

Since OS X 10.11 in the new NSCollectionView you should use instead:

因为在新的NSCollectionView中,OS X 10.11应该使用以下内容:

[self.collectionView scrollToItemsAtIndexPaths:[self.collectionView selectionIndexPaths] scrollPosition:NSCollectionViewScrollPositionCenteredHorizontally];

#3


0  

let scrollPositionX: CGFloat = videoThumbCollectionView.visibleRect.origin.x + 150.0 videoThumbCollectionView.scroll(NSPoint(x: scrollPositionX, y: videoThumbCollectionView.visibleRect.origin.y))

让scrollPositionX: CGFloat = videoThumbCollectionView.visibleRect.origin。videoThumbCollectionView x + 150.0。滚动(NSPoint(x:scrollPositionX y:videoThumbCollectionView.visibleRect.origin.y))