NSCollectionViewItem子类中的自定义插件 [英] Custom outlets in NSCollectionViewItem subclass

查看:560
本文介绍了NSCollectionViewItem子类中的自定义插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个简单的任务,但我似乎不能使它的工作。
我试图有一个NSCollectionView与自定义项目。我添加了另一个NSImageView到项目的自定义视图,并且我继承这个视图为了添加连接到这个额外的NSImageView的自定义插座。

I feel this being a simple task, but I don't seem to be able to make it work. I'm trying to have a NSCollectionView with custom items. I added another NSImageView to the custom view of the item, and I subclassed this view in order to add the custom outlet connected to this additional NSImageView.

现在我覆盖 - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object 因为有时我需要删除此NSImageView。

Now I am overriding - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object because sometimes I need to remove this NSImageView.

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object {

    CustomItem *theItem = (CustomItem *)[super newItemForRepresentedObject: object];

    ...

    if (I need to remove that NSImageView) {

        [[theItem additionalImageView] removeFromSuperview];

    }

    return theItem;

}



无论如何,additionalImageView似乎是 (nil)。这是显而易见的,因为super方法将返回默认的NSCollectionViewItem没有自定义outlet。

Anyway, additionalImageView seems to be (nil). This is someway obvious because the super method will return the default NSCollectionViewItem which has not the custom outlet.

在这里做最好的事情是什么?我阅读了 copy 方法,我尝试了:

What's the best thing to do right here? I read something about the copy method, and I tried with:

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object {

    CustomItem *theItem = [(CustomItem *)[super itemPrototype] copy]; // Here is the change

    ...

    if (I need to remove that NSImageView) {

        [[theItem additionalImageView] removeFromSuperview];

    }

    return theItem;

}

但是这不行。因此,是否有使用自定义NSCollectionViewItem保留自定义插座的方法?

任何帮助将非常感谢。谢谢!

Any help would be very appreciated. Thank you!

推荐答案

问题是没有人会实例化新项目的图像视图。

The problem is that no one will instantiate the new item's image view. Copy won't work, since you need two image views, not one.

有两种处理方式:


  1. 不要调用 newItemForRepresentedObject 的超类实现,而是使用 NSNib 自己实例化项目(下面的工厂方法)。在方法调用中,您可以指定 self 作为所有者,它将为您连接插座。然后设置resentObject ,并与图像视图一起使用。这里是工厂方法的代码:

  1. Instead of calling the superclass implementation of newItemForRepresentedObject, use NSNib to instantiate the item yourself (factory method below). In the method call, you can specify self as the owner, and it will hook up the outlets for you. Then set representedObject and fiddle with the image view. Here's code for the factory method:

// Load item view from CustomItem.nib
// For consistent results, nib should contain exactly one NSCollectionViewItem.
- (NSCollectionViewItem *)newCollectionViewItem {
    static NSNib *nib;
    if (!nib) nib = [[NSNib alloc] initWithNibNamed:@"CustomItem" bundle:nil];

    NSArray *nibObjects;
    if (![nib instantiateNibWithOwner:self topLevelObjects:&nibObjects]) return nil;

    for (id obj in nibObjects)
        if ([obj isKindOfClass:[NSCollectionViewItem class]])
            return (NSCollectionViewItem *)obj;

    return nil;
}


  • 调用 [super newItemForRepresentedObject: ] ,请检查您是否需要保留图片视图。如果你这样做,实例化一个新的 NSImageView ,设置其属性,并将其添加到superview。最后一部分听起来很棘手。也许某人采用这种方法会提供一些代码。

  • After you call [super newItemForRepresentedObject:], check if you need to keep the image view. If you do, instantiate a new NSImageView, set its properties, and add it to the superview. That last part sounds tricky. Maybe someone who's taken that approach will provide some code.

    这篇关于NSCollectionViewItem子类中的自定义插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆