【问题标题】:How to retrieve NSCollectionViewItems from a NSCollectionView如何从 NSCollectionView 中检索 NSCollectionViewItems
【发布时间】:2011-06-28 14:56:30
【问题描述】:

我刚刚实现了一个 NSCollectionView,就像 developer page 中描述的那样,它运行良好。

现在,我如何从 CollectionView 访问 collectionViewItems?

【问题讨论】:

  • 为什么需要实际的NSCollectionViewItems?他们所代表的对象不应该包含您需要的所有信息吗?
  • 想象一个单一的选择行为:当我选择一个项目时,该项目向collectionView发送一个事件,collectionView告诉其他项目改变它的状态。
  • 集合项可以检测到它何时被点击,并发布通知让其他集合项做出适当的响应。
  • 我的 collectionView 将充当他的项目的观察者/控制器。因此,它将决定何时向项目发送消息。

标签: objective-c cocoa cocoa-bindings


【解决方案1】:

如果您真的需要枚举集合视图中的所有项目,那么:

NSUInteger numberOfItems = [[collectionView content] count];
for (NSUInteger itemIndex = 0; itemIndex < numberOfItems; itemIndex++) {
    NSCollectionViewItem *item = [collectionView itemAtIndex:itemIndex];
    // do something with item
}

应该在 Mac OS X v10.6+ 上解决问题。

【讨论】:

    【解决方案2】:

    斯威夫特 5.0

    let allSections = self.collectionView.numberOfSections
          (0..<allSections).forEach { aSection in
            let items = self.collectionView.numberOfItems(inSection: aSection)
            (0..<items).forEach { item in
                let indexpath = IndexPath(item: item, section: aSection)
                if let item = self.collectionView.item(at: indexpath) {
                    print(item)
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多