How to retrieve NSCollectionViewItems from a NSCollectionView
I just implemented an NSCollectionView just like the one described on the developer page and it work perfec开发者_如何学JAVAtly.
Now, how can I access to collectionViewItems from CollectionView?
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+.
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)
}
}
}
精彩评论