gpt4 book ai didi

objective-c - NSOutlineView 示例

转载 作者:太空狗 更新时间:2023-10-30 03:21:45 25 4
gpt4 key购买 nike

拜托,你能给出一个简单完整的 Cocoa 应用程序示例吗?NSOutlineView 的分层数据表示不像 NSOutlineView and NSTreeController example 那样模棱两可。.

谢谢!

最佳答案

这个例子使用了一个简单的两层层次结构,两个父 Foo 和 Bar 分别有两个 child ,分别是 Foox、Fooz 和 Barx、Barz,大纲 View 是默认的,只有两列和第二列的标识符设置为“ child ”。

NSDictionary *firstParent = @{@"parent": @"Foo", @"children": @[@"Foox", @"Fooz"]};
NSDictionary *secondParent = @{@"parent": @"Bar", @"children": @[@"Barx", @"Barz"]};
NSArray *list = @[firstParent, secondParent];

- (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:@"%i kids",[[item objectForKey:@"children"] count]];
}
return item;
}else{
if ([item isKindOfClass:[NSDictionary class]]) {
return [item objectForKey:@"parent"];
}
}

return nil;
}

screenshot

关于objective-c - NSOutlineView 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com