无法获取选定的集合视图项

时间:2015-01-11 15:52:50

标签: objective-c xcode macos cocoa

我在尝试使用Xcode 6选择我的集合视图并尝试构建OSX应用程序时遇到问题。

我按照http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/CollectionViews/Introduction/Introduction.html处的说明创建了集合视图,它按预期工作但我无法选择我点击的项目。我确保在IB中可以选择视图。

我实现了Selection Highlight in NSCollectionView中提到的通知方法,我可以看到当我填充事件触发的集合视图时,无论我在集合视图项中单击何处,通知都不会再次触发。

正如我认为正常的集合视图一样,我只是试图获取所选项目的数组索引,以便我可以显示详细信息。

我在网上试图找到解决方案,但绝大多数解决方案都是针对使用segue的IOS,而不是针对OSX。例外情况是我为Stack Overflow发布的链接。

我甚至放了一个覆盖整个集合视图项目的透明按钮,这样我就可以抓住一个点击事件(虽然有效,但我仍然不知道点击了哪个项目)。

我的问题是:如何获取我在集合视图中单击的数组项?

enter image description here

1 个答案:

答案 0 :(得分:2)

了解有关对阵列控制器selectionIndexes的更改的一种方法是将此属性绑定到代码中的NSIndexSet实例,然后使用KVO设计模式在此时请求通知{ {1}}已被更改。如果您正确设置此选项,当用户单击NSIndexSet中未选中的单元格时,阵列控制器用于存储其选择索引的NSCollectionView将更新。由于此索引集是您正在观察的索引集,因此您将收到通知,告知您有关更改的信息。

在我为回答这个问题而创建的demo-app中,我在NSIndexSet上放置了有问题的索引集 - 这是实现文件:

AppDelegate

// Interface //////////////////////////////////////////////////////// @interface AppDelegate () // This is the content array from which the NSArrayController will derive // it's arrangedObjects array @property (nonatomic, strong) NSArray *collectionViewContent; // This is the NSIndexSet that I want the NSArrayController to use // to store its selectionIndexes. @property (nonatomic) NSIndexSet *mySelectionIndexes; @end // Implementation ////////////////////////////////////////////////// @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // The content that will be shown in the NSCollectionView (each word // represents a single collection view item). self.collectionViewContent = @[@"The", @"rain", @"in", @"Spain", @"falls", @"..."]; // Tell cocoa you want to know when the array controller makes changes to // the index set it's using to stores its selection indexes [self addObserver:self forKeyPath:@"mySelectionIndexes" options:0 context:nil]; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"Collection view selection just changed to: %@", self.mySelectionIndexes); } ////////////////////////////////////////////////////////////////// Bindings Inspector 中,您现在可以告诉阵列控制器使用NSArrayController属性AppDelegate来存储其选择索引:

enter image description here

如果您仍然遇到问题,可能是因为其他地方的绑定出错 - 这是我使用的所有绑定:

<强> NSArrayController的 数组控制器将从mySelectionIndexes对象管理的数组中获取内容。它会将其选择索引存储在AppDelegate中,也由NSIndexSet

管理
  1. 内容数组:App Delegate.collectionViewContent
  2. 选择索引:App Delegate.mySelectionIndexes
  3. <强> NSCollectionView 集合视图将从阵列控制器获取它的模型数据。它将标记为 selected 那些出现在存储在数组控制器的AppDelegate属性中的索引的视图:

    1. 内容:Array Controller.arrangedObjects
    2. 选择索引:Array Controller.selectionIndexes
    3. 在我将selectionIndexes拖到画布上时自动生成的视图上,我添加了一个NSCollectionView,此文本字段只有一个绑定:

      1. 值:集合查看Item.representedObject(换句话说,在模型密钥路径字段中键入 representObject
      2. 最后一句话:

        值得指出的是,你没有拥有来设置此绑定。要在用户选择或取消选择集合视图中的某个项目时获取单词,请创建NSTextField的子类并覆盖NSCollectionViewItem setter。每次选择或取消选择项时,都会自动调用此属性。在您的实施中,您现在可以对项目的视图进行调整,以考虑其状态已更改的事实。在我的演示应用程序中,selected的自定义子类名为NSCollectionViewItem

        PPCollectionViewItem
相关问题