• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Images in NSOutlineView disappear on dataReload
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Images in NSOutlineView disappear on dataReload


  • Subject: Images in NSOutlineView disappear on dataReload
  • From: Nick <email@hidden>
  • Date: Fri, 13 Nov 2015 19:48:09 +0200

Hello
I've encountered a strange behavior in a source list NSOutlineView.
Basically, whenever I call "reloadData", an image, that was set for a
currently selected row (NSTableCellView's imageview.image), disappears (I
set image in outlineView:viewForTableColumn:item:). When I select the row,
the image appears again, as on the pictures:

Select a row, press "Button" to reloadData (the image disappears)
http://i.imgur.com/l4rmOrl.png
Select the row again (the image appears again)
http://i.imgur.com/LrC1BVy.png


I tried to "troubleshoot" this in various ways, even using F-Script (which
revealed that NSOutlineView changes NSTableCellView's imageview.image class
automatically from NSImage to an "internal" class NSSidebarImage after
reload). But other than that I cannot understand, why the image disappears.
Is there anything wrong with my code (although it is almost identical to
Apple's outline view sample code), or is it a bug in the AppKit?

Here's a very simple sample code that shows the problem. I just created a
new XCode project, added a Source List control to the window, and set
outline view's dataSource and delegate to my AppDelegate (also created the
IBOutline for the Source List and call it sidebarOutlineView).

Anyway, thank you for taking a look at this!

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property NSArray *topLevel;
@property NSDictionary<NSString*, NSArray*> *topLevelToSecondLevelDict;
@property (weak) IBOutlet NSOutlineView *sidebarOutlineView;
@end


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.topLevel = @[@{@"title": @"Header1",
                            @"isRootItem": @YES},
                            @{@"title": @"Header2",
                            @"isRootItem": @YES}
                           ];

    NSArray *subitems1 = @[@"11", @"12"];
    NSArray *subitems2 = @[@"21", @"22"];
    self.topLevelToSecondLevelDict = @{@"Header1": subitems1,
                                       @"Header2": subitems2};

    [_sidebarOutlineView reloadData];

    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0];
    [_sidebarOutlineView expandItem:nil expandChildren:YES];
    [NSAnimationContext endGrouping];
}


- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index
ofItem:(id)item {

    if(!item) {
        return self.topLevel[index]; //returns an NSDictionary, that
corresponds to the root item
    } else {
        return [self.topLevelToSecondLevelDict[[item
objectForKey:@"title"]][index]
copy];
    }
}


- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    if([item isKindOfClass:[NSDictionary class]]) {
        return YES;
    } else {
        return NO;
    }
}


- (NSInteger) outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item {
    if(!item) {
        return self.topLevel.count;
    } else {
        return [self.topLevelToSecondLevelDict[[item objectForKey:@"title"]]
count];
    }
}


- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
    return [self.topLevel containsObject:item];
}


- (NSView *)outlineView:(NSOutlineView *)outlineView
viewForTableColumn:(NSTableColumn
*)tableColumn item:(id)item {
    if ([self.topLevel containsObject:item]) {
        NSTableCellView *result = [outlineView
makeViewWithIdentifier:@"HeaderCell"
owner:self];
        result.textField.stringValue = [[item objectForKey:@"title"] copy];
        return result;
    } else  {
        NSTableCellView *result = [outlineView
makeViewWithIdentifier:@"DataCell"
owner:self];
        result.textField.stringValue = [item copy];
        NSString *parentTitle = [[outlineView parentForItem:item]
objectForKey:@"title"];
        if([parentTitle isEqualToString:@"Header1"]) {
            result.imageView.image = [NSImage imageNamed:
NSImageNameHomeTemplate];
        } else if([parentTitle isEqualToString:@"Header2"]) {
            result.imageView.image = [NSImage imageNamed:
NSImageNameListViewTemplate];
        } else {
            NSLog(@"Error!");
        }
        return result;
    }
}


- (IBAction)button:(id)sender {
    [self.sidebarOutlineView reloadData];
}

@end
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Images in NSOutlineView disappear on dataReload
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: Identify image file count in directory
  • Next by Date: Re: Customising NSFontManager
  • Previous by thread: Re: Identify image file count in directory
  • Next by thread: Re: Images in NSOutlineView disappear on dataReload
  • Index(es):
    • Date
    • Thread