實現這一委託並使用類似的東西:
- (void)outlineViewItemDidExpand:(NSNotification *)notification{
for (id parent in list) {
if ([notification.userInfo objectForKey:@"NSObject"] == parent) {
continue;
}
[notification.object collapseItem:parent];
}
}
這裏parent
是頂級項目陣列,如在你的例子Folder1中和文件夾2。
編輯:
Appdelegate.h
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property(strong) NSDictionary *firstParent;
@property(strong) NSDictionary *secondParent;
@property(strong) NSArray *list;
@end
Appdelegate.m
@implementation AppDelegate
@synthesize firstParent;
@synthesize secondParent;
@synthesize list;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (id)init {
self = [super init];
if (self) {
firstParent = [NSDictionary dictionaryWithObjectsAndKeys:@"A",@"parent",
[NSArray arrayWithObjects:@"Apple",@"Aeroplane", nil],@"children", nil];
secondParent = [NSDictionary dictionaryWithObjectsAndKeys:@"B",@"parent",
[NSArray arrayWithObjects:@"Ball",@"Baloon", nil],@"children", nil];
list = [NSArray arrayWithObjects:firstParent,secondParent, nil];
}
return self;
}
- (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 == nil) { //item is nil when the outline view wants to inquire for root level items
return [list count];
}
if ([item isKindOfClass:[NSDictionary class]]) {
return [[item objectForKey:@"children"] count];
}
return 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{
if (item == nil) { //item is nil when the outline view wants to inquire for root level items
return [list objectAtIndex:index];
}
if ([item isKindOfClass:[NSDictionary class]]) {
return [[item objectForKey:@"children"] objectAtIndex:index];
}
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{
if ([[theColumn identifier] isEqualToString:@"children"]) {
if ([item isKindOfClass:[NSDictionary class]]) {
return [NSString stringWithFormat:@"%ld kids",[[item objectForKey:@"children"] count]];
}
return item;
}
else{
if ([item isKindOfClass:[NSDictionary class]]) {
return [item objectForKey:@"parent"];
}
}
return nil;
}
隨着多一個寫在頂部方法。
然後在IB中將NSOutlineView的代理設置爲AppDelegate。
所有摺疊方法都被成功調用,但實際的公開內容沒有被取消選擇。我的意思是說,父母不會被關閉。 – Xander
讓我發佈我的完整代碼.... –
是的..將等待如此 – Xander