I'm trying to create a source list for a new program and I'm having a little issue using a view-based NSOutlineView. My code works fine using a cell-based NSOutlineView so I'm a little confused about what is happening.
Here is my code for the delegate and data source:
#pragma mark -
#pragma mark NSOutlineView Delegate
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return (item == nil) ? YES : [(SourceListNode *)item groupItem];
}
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([(SourceListNode *)item groupItem]) {
return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
return [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
}
#pragma mark -
#pragma mark NSOutlineView Data Source
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? [sourceListNodes count] : [(SourceListNode *)item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : ([(SourceListNode *)item numberOfChildren] > 0);
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [sourceListNodes objectAtIndex:index] : [(SourceListNode *)item childAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [(SourceListNode *)item name];
}
When I run this through the debugger it see that outlineView:objectValueForTableColumn:byItem:
does not execute. The result is an outline view with no text. The correct cells are created and I am able to select them and expand them. What am I missing?
UPDATE: I deleted my source list in IB, added a new one, and connected it to my controller object. The results were better, but the header cells had the text "HEADER CELL" and the child cells had "Table View Cell".
I ran the program through the debugger again and this time outlineView:objectValueForTableColumn:byItem:
was executed. It did not populate the text of the cell though.
I then updated outlineView:viewForTableColumn:item:
as follows:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *result;
if ([(SourceListNode *)item groupItem]) {
result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
[[result textField] setStringValue:[(SourceListNode *)item name]];
return result;
}
Now everything works as expected.
So in short, I have solved my problem. However, I now have another question. What is the purpose of outlineView:objectValueForTableColumn:byItem:
for view-base outline views? It executes, but does not appear to do anything.
View-based is a tad different than cell-based.
All that is different is replacing the "id" return value to "NSView".
Change this:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
To this:
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
This is given the assumption you are running 10.7, and you are actually going to return an NSView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With