我有一个NSCollectionView
显示一些图像。我实施了一个NSCollectionViewDelegate
来告诉它应该选择和/或突出显示哪些项目。我正在使用库存NSCollectionViewItem
来绘制图像及其名称。当用户选择一个项目,我代表得到消息关于高亮度状态的变化:如何在NSCollectionView中正确显示当前选择?
- (void)collectionView:(NSCollectionView *)collectionView
didChangeItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
toHighlightState:(NSCollectionViewItemHighlightState)highlightState
{
[collectionView reloadItemsAtIndexPaths:indexPaths];
}
我为didSelect
/didDeselect
做类似的事情:
- (void)collectionView:(NSCollectionView *)collectionView
didSelectItemsAtIndexPaths:(nonnull NSSet<NSIndexPath *> *)indexPaths
{
[collectionView reloadItemsAtIndexPaths:indexPaths];
}
在NSCollectionViewItem
小号view
,我做的以下:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSColor* bgColor = [[self window] backgroundColor];
NSColor* highlightColor = [NSColor selectedControlColor];
NSRect frame = [self bounds];
NSCollectionViewItemHighlightState hlState = [collectionViewItem highlightState];
BOOL selected = [collectionViewItem isSelected];
if ((hlState == NSCollectionViewItemHighlightForSelection) || (selected))
{
[highlightColor setFill];
}
else
{
[bgColor setFill];
}
[NSBezierPath fillRect:frame];
}
我看到的问题是绘制突出显示或选择似乎是跑DOM。当它绘制选择时,它几乎总是在用户实际选择的项目上(尽管由于某种原因通常会遗留最后一个项目)。偶尔,它会选择用户没有点击或拖动的其他项目。但是,通常情况下,它不会画出来。
我已添加打印以确认它正在呼叫-didChangeItemsAtIndexPaths:toHighlightState:
和-didSelectItemsAtIndexPaths:
。有什么我在这里做错了吗?
我已经添加了一些记录到视图的-drawRect:
方法,并没有出现有愈演愈烈呼吁所有过渡,即使我打电话的-didChange*
方法-reloadItemsAtIndexPaths:
。为什么不?
我也注意到,尽管-should/didSelectItemsAtIndexPaths:
确实被调用,但代理的-should/didDeselectItemsAtIndexPaths:
似乎并没有被调用过。这是为什么?
试试这个链接可能会帮助你http://stackoverflow.com/questions/2541572/selection-highlight-in-nscollectionview?rq=1 –