NSOutlineView Problem with Child / Parent element
I am implementing NSOutlineView and having implemented following method,
-(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];
}
And Implemented following delegate Method
- (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
}
Some other method
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
and get data
i am following some blogs , tutorial, the problem what i am facing is as par documentation it should hit in numberOfChildrenOfItem for each element where we need to calculate and send the No of child for that item, but the problem what i am facing, its coming to above function only once and for that too item is nil, i.e. its not coming 开发者_高级运维for other element, Am i missing any method that needs to be delegate
other method which i override is as below,
- (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
Please tell me , which method i am missing
In outlineView:numberOfChildrenOfItem:
, you need to test if item is nil
and, if so, return the number of children of the top level object. Quoting the documentation,
If item is
nil
, this method should return the number of children for the top-level item.
If you’re returning 0
when item is nil
, the outline view considers that there are no children for the top level item, hence it won’t send this message again to your data source object.
@Rohan did you implemented the datasource methods or not.
Check out the documentation for the NSOutlineView
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSOutlineView_Class/Reference/Reference.html
精彩评论