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

╄→гoц情女王★ 提交于 2019-12-12 08:07:52

问题


I am wanting something similar to how iWork has the template selection screen for Pages when you can select different templates, and each view contains different info has difference sizes etc.

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

personView and companyView are properties in the subclassed NSCollectionView, that are IBOutlets to views 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;

}


回答1:


(It doesn't even seem possible to make an NSCollectionView with differently-sized item views; each size would need to be a multiple or integer divisor of some "main" size, and you'd need to do massive item-checking and -reordering to be sure it's even possible to render them in a grid. Are you sure you're asking the right question?)

Also, I don't see anything like this in iWork: all the views in its template chooser are the same. (Though their NSImageView subviews are of different sizes.) I'd recommend if at all possible using the same view and changing its subviews appropriately. It's easy to, for example, bind text fields' "hidden" property or change the width of an image view. Can't you make a single view that works for both classes, changing itself appropriately depending on the represented object?



来源:https://stackoverflow.com/questions/2308798/is-there-a-way-to-have-varying-views-in-an-nscollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!