objective-c - NSOutlineView显示三个层次

标签 objective-c xcode cocoa nsoutlineview

我正在使用 NSOutlineView,我想显示第四个子级别。现在我只能显示三个子级别。

outlineViewController.m

 -(id)init{
     self = [super init];
     if(self){
         _people = [[NSMutableArray alloc]init];

         Person *quick = [[Person alloc]initWithName:@"First"];
         [quick addChild:[[Person alloc]initWithName:@"Second"]];
         [(Person *)[quick.children objectAtIndex:0]addChild:[[Person alloc]initWithName:@"Third"]];

         [_people addObject:quick];

     }
     return self; 
 }

人.m

-(id)init{
     return [self initWithName:@"Name"]; 
 }


 -(id)initWithName:(NSString *)name {
         self = [super init];
         if(self){
             _name = [name copy];
             _children = [[NSMutableArray alloc]init];

         }
         return self; 
     }

 -(void)addChild:(Person *)p {
     [_children addObject:p];
 }

person.h

 @property (copy)NSString *name;
 @property(readonly,copy)NSMutableArray *children;
 -(id)initWithName:(NSString *)name;
 -(void)addChild:(Person *)p;

我得到的输出是这样的。

>First
   >Second
      Third

我想要这样的输出..

>First
   >Second
      >Third
         >Fourth
            Fifth

谢谢。

最佳答案

您要在此处将一个 child 添加到第二个:

[(Person *)[quick.children objectAtIndex:0]addChild:[[Person alloc]initWithName:@"Third"]];

同样,您可以将子项添加到第三个:

[(Person*)[((Person *)[quick.children objectAtIndex:0]).children objectAtIndex:0]addChild:[[Person alloc]initWithName:@"Fourth"]];

然后到第四个:

 [(Person*)[((Person*)[((Person *)[quick.children objectAtIndex:0]).children objectAtIndex:0]).children objectAtIndex:0] addChild:[[Person alloc]initWithName:@"Fifth"]];

或者为了简单起见,首先创建最低级别的对象,然后将其作为子对象添加到其父对象中:

Person *fifth = [[Person alloc]initWithName:@"Fifth"]; 
Person *fourth = [[Person alloc]initWithName:@"Fourth"];
[fourth addChild: fifth];
Person *third = [[Person alloc]initWithName:@"Third"];
[third addChild: fourth];
Person *second = [[Person alloc]initWithName:@"Second"];
[second addChild: third];
Person *quick = [[Person alloc]initWithName:@"First"];
[quick addChild: second];

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

相关文章:

iphone - UITableView 怪异!带图片

objective-c - 什么时候应该将 header 导入 Prefix.pch?

objective-c - 分离 SQLite 数据库时出错 - 数据库已锁定

objective-c - 获取 NSMutableDictionary 中的键出现在弹出按钮中

cocoa - Xcode Cocoa QTKit - 创建引用文件

ios - UITableView 在上方和下方添加空行,在将页眉和页脚设置为空 View 后仍然存在

ios - 获取 UnityException : Launching iOS project via Xcode4 failed

ios - 在表格 View 单元格中添加自动布局约束。

xcode - 如何在 swift spritekit 中对 SKShapeNode 进行分组

objective-c - cocoa 怎么样 : addObserver working under the hood?