2011-01-29 44 views
-1

我實現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 

和獲取數據

我正在關注一些博客,教程, 我面臨的問題是作爲參數文檔,它應該在numberOfChildrenOfItem中爲我們需要計算的每個元素髮送併發送該項目的子項號爲 ,但問題是什麼它正在面對,它的上述功能只有一次,對於這個項目是零,即它不來其他元素, 我是否缺少任何方法,需要委託

我重寫的其他方法如下,

- (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

outlineView:numberOfChildrenOfItem:,你需要測試,如果產品nil,如果是這樣,則返回頂級對象的兒童人數。引述documentation

如果項目nil,此方法應該返回的兒童人數爲頂級項。

如果你返回0當產品nil,大綱視圖認爲有沒有孩子的頂級項目,因此它不會再次發送此郵件到您的數據源對象。

+0

你好,非常感謝,我錯過了一種方法來實現 - (void)isGroupItem,所以它沒有設置爲一個組項目 – Amitg2k12 2011-01-30 11:18:42