Is there a way to have different views in an NSCollectionView?

I want something similar to how iWork has a template selection screen for pages, when you can select different templates, and each view contains different information, has difference sizes, etc.

I tried to subclass NSCollectionView and determine which view to display using the newItemForRepresentedObject method (as opposed to using the itemPrototype Interface Builder), but for some reason does not position the views correctly and does not show the correct number of views for the number of items present. Here is my code. I was hoping someone might have a better way to do this, or an example of how this is done.

personView and companyView are properties in the NSCollectionView subclass that are IBOutlets to view in IB.

-(NSCollectionViewItem *)newItemForRepresentedObject:(id)object{ NSCollectionViewItem *collectionViewItem = [[NSCollectionViewItem alloc] init]; [collectionViewItem setRepresentedObject:object]; if([[object valueForKey:@"company"] boolValue] == YES){ NSView *view = [companyView retain]; [collectionViewItem setView:companyView]; }else{ [collectionViewItem setView:personalView]; } return collectionViewItem; 

}

+6
objective-c cocoa macos nscollectionview
source share
1 answer

(It’s not even possible to create an NSCollectionView with views of different sizes, each size must be a multiple or integer divider of some β€œmain” size, and you will need to do a massive element, check and -reordering to make sure that it is even possible to do this them in the grid. Are you sure you are asking the right question?)

Also, I don't see anything like this in iWork: all the views in its selection template are the same. (Although their NSImageView subordinates have different sizes.) I would recommend, if possible, using the same overview and changing their routines accordingly. It is easy, for example, to associate text fields with a "hidden" property or change the width of the image. Can't you make one view that works for both classes, changing it accordingly depending on the represented object?

0
source share

All Articles