有没有办法在 NSCollectionView 中拥有不同的视图?

发布于 2024-08-22 13:49:46 字数 813 浏览 6 评论 0原文

我想要类似于 iWork 具有页面模板选择屏幕的方式,当您可以选择不同的模板时,每个视图包含不同的信息,具有不同的大小等。

我尝试对 NSCollectionView 进行子类化并使用 newItemForRepresentedObject 方法确定要显示的视图(如与使用 itemPrototype 视图 Interface Builder 相反),但由于某种原因,它无法正确定位视图,并且它不会显示当前项目数量的正确视图数量。这是我的代码。我希望有人可以有更好的方法来做到这一点,或者是如何完成此操作的示例。

personView和companyView是子类NSCollectionView中的属性,它们是IB中视图的IBOutlet。

-(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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-08-29 13:49:47

(似乎甚至不可能制作具有不同大小的项目视图的 NSCollectionView;每个大小都需要是某个“主”大小的倍数或整数除数,并且您需要进行大量项目检查 -重新排序以确保甚至可以在网格中渲染它们。您确定您问的是正确的问题吗?)

另外,我在 iWork 中没有看到类似的内容:其模板选择器中的所有视图都是相同的。 (尽管它们的 NSImageView 子视图大小不同。)我建议如果可能的话使用相同的视图并适当地更改其子视图。例如,绑定文本字段的“隐藏”属性或更改图像视图的宽度很容易。难道你不能创建一个适用于这两个类的单一视图,根据所表示的对象适当地改变自身吗?

(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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文