【问题标题】:NSOutlineView displays three levelsNSOutlineView 显示三个级别
【发布时间】:2013-11-13 05:16:04
【问题描述】:

我正在使用 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];
 }

人.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

谢谢。

【问题讨论】:

  • 你正在添加三个孩子,这就是为什么你要获得三个级别,将一个孩子进一步添加到第三个然后到第四个
  • 我可以知道如何将孩子添加到孩子吗?

标签: objective-c xcode cocoa nsoutlineview


【解决方案1】:

你在这里添加一个孩子到第二个:

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

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

[(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];

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多