Custom outlets in NSCollectionViewItem subclass

时光毁灭记忆、已成空白 提交于 2019-12-01 09:18:08

问题


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.

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;

}

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.

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;

}

But this is not going to work. So, is there a way to preserve custom outlets when using a custom NSCollectionViewItem?

Any help would be very appreciated. Thank you!


回答1:


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.

There are two ways to handle this:

  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;
    }
    
  2. 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.




回答2:


One way to solve the problem is to create 2 different prototype views and bind them to your controller. Now when you are in the newItemForRepresentedObject you can set the view of your NSCollectionViewItem with a copy (using the NSCoding protocol) of the appropriate prototype depending on the object. I use the NSCollectionViewItem I get from super.

Here My sample code for the casa of showing a string or a number attribute editor of an unknown NSManagedObject, NSManagedAttribute is a utility class that I use as represented object for my collection view and keeps information about the attribute/relationship

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object{
    NSManagedAttribute *attribute = object;
    NSCollectionViewItem *item =[super newItemForRepresentedObject:object];
    NSData * archivedView = nil;
    if (attribute.attributeType == NSInteger16AttributeType){
        archivedView = [NSKeyedArchiver archivedDataWithRootObject:self.controller.integerView];
    } else if (attribute.attributeType == NSStringAttributeType) {
        archivedView = [NSKeyedArchiver archivedDataWithRootObject:self.controller.stringView];
    }
    NSView * viewCopy = [NSKeyedUnarchiver unarchiveObjectWithData:archivedView];
    item.view = viewCopy;
    return item;
}

It is working for me :-)



来源:https://stackoverflow.com/questions/6884352/custom-outlets-in-nscollectionviewitem-subclass

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