Expanding all items of a NSOutlineView that loads data from a data source

Deadly 提交于 2021-02-04 12:40:08

问题


First of all I'm new to cocoa development so I suppose I'm probably trying to do this the wrong way, but here goes:

I have a NSOutlineView which loads the data from a NSOutlineViewDataSource implementation. I want all the items to be expanded after they are loaded, but i can't seem to find an event fired when the data has finished loading, so I can send a [outlineView expandItem: nil expandChildren: YES] to it.

I looked into the NSOutlineViewDelegate protocol but I was unable to find a suitable place for this call. What would be the best approach for this problem?


回答1:


Normally I like to do something like this inside form - awakeFromNib or any other startup callbacks

dispatch_async(dispatch_get_main_queue(), ^{
    [self.outlineView expandItem:root expandChildren:YES];
});

This will enqueue the execution block at the end of the current cycle in the runloop, thus, it will be executed after all initialization has taken place. There's no need to set any artificial delay.




回答2:


The best solution I've come up with is to write a method that expands the NSOutlineView after a delay of zero.

- (void)windowDidLoad
{
    [super windowDidLoad];
    [self performSelector:@selector(expandSourceList) withObject:nil afterDelay:0.0];
}

- (IBAction)expandSourceList
{
    [mSourceListView expandItem:nil expandChildren:YES];
}



回答3:


I found the answer. It seems that implementing the delegate method -(void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item will do the trick:


-(void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    [outlineView expandItem:item];
}



回答4:


Here's how I normally handle this. I like to show my main window myself rather than letting it happen automatically. This allows me to make sure all of my interface items are setup how I want before I show the user the window. It seems you could do this too. So first I uncheck "Visible at launch" in interface builder for the window. Then in my application's delegate class I use this method which is a deleate method of NSApplication:

  • (void)applicationDidFinishLaunching:(NSNotification *)aNotification

In there I setup my interface items because at that point I know everything is loaded. And then the last line of that method would be: [myWindow makeKeyAndOrderFront:self];. This way you know your window is perfect before your user sees the window. So I would try your method there.



来源:https://stackoverflow.com/questions/2631207/expanding-all-items-of-a-nsoutlineview-that-loads-data-from-a-data-source

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