带有Child / Parent元素的NSOutlineView问题

时间:2011-01-29 08:41:19

标签: objective-c cocoa macos nsoutlineview

我正在实施NSOutlineView并实施了以下方法,

    -(void) initOutlineView{

        pMyOutlineView       = [[[MyUICustomOutlineView alloc] initWithFrame:clipViewBounds]          
                                autorelease];

    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [firstColumn setWidth:25];
    [pMyOutlineView  addTableColumn:firstColumn];

    NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];
        NSTextFieldCell *pCell = [[NSTextFieldCell alloc]init];

        [secondColumn setDataCell:pCell];
        [secondColumn setWidth:180];

        [pMyOutlineView  addTableColumn:secondColumn];
        [pMyOutlineView setRowHeight:30];


        pNodeArray = [[NSMutableArray alloc]initWithCapacity:10];

        PointerNode *pNode = pointerList.getHead();

        int idx =0;
        void *ptr = nil;
        while ( contact ) {
            [pNodeArray insertObject:[NSValue valueWithPointer:(void *)pNode] 
                atIndex:idx];
            pNode = pNode->getNext();
            idx++;

        }

        [pMyOutlineView setDataSource:self];
        // this is to tell myCustomOutlineView to delegate Menu and Mouse event to 
        // this interface
        [pMyOutlineView setDataDelegate:self];
        [scrollView setDocumentView:pMyOutlineView];

        [pMyOutlineView setDelegate:self];


}

并在委托方法

之后实施
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {

        // Here it will come for the top level element 
    if(item==nil) 
        return pointerList.size();

    /* 
     If its not NULL then it must be some child element 
     */
    if([item isKindOfClass:[NSValue class]])
    {
                // yes it may have children 
        PointerNode *pNode = (PointerNode *)[item pointerValue];
        if(pNode->hasChildren()){
            return pNode->getNoOfChild();
        } 
    }
    return 0; // means this element not going to have any children 
}

其他一些方法

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item

获取数据

我正在关注一些博客,教程, 我面临的问题是作为par文档,它应该在我们需要计算的每个元素的numberOfChildrenOfItem中命中,并发送该项目的子项的No, 但问题是我所面临的问题,它只能进入上述功能一次而且这个项目也是零,即它不会用于其他元素, 我错过了任何需要委托的方法

我覆盖的其他方法如下,

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)aTableColumn byItem:(id)item

- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item

请告诉我,我缺少哪种方法

2 个答案:

答案 0 :(得分:3)

outlineView:numberOfChildrenOfItem:中,您需要测试项目是否为nil,如果是,则返回顶级对象的子项数。引用documentation

  

如果nil,则此方法应返回顶级项目的子项数。

如果您在项目为0时返回nil,则大纲视图会认为顶级项目没有子级,因此它不会再将此消息发送到您的数据源对象

答案 1 :(得分:0)