4

I just implemented an NSCollectionView just like the one described on the developer page and it work perfectly.

Now, how can I access to collectionViewItems from CollectionView?

Giuseppe
  • 6,666
  • 2
  • 20
  • 32
  • Why do you need the actual `NSCollectionViewItems`? Shouldn't their represented objects have all the info you need? – Dave DeLong Jun 28 '11 at 15:01
  • Imagine for example a single selection behaviour: when I select an item, the item send an event to collectionView, and collectionView tell to others items to change its status. – Giuseppe Jun 28 '11 at 15:06
  • the collection item could detect when it was clicked and post a notification for the other collection items to respond to appropriately. – Dave DeLong Jun 28 '11 at 15:10
  • My collectionView will act as an observer/controller for his items. So, it will decide whenever to send messages to items. – Giuseppe Jun 28 '11 at 15:15

2 Answers2

6

If you really need to enumerate all items in a collection view, then:

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

should do the trick on Mac OS X v10.6+.

0

Swift 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)
            }
        }
    }
K_Mohit
  • 528
  • 3
  • 17