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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to