Subviews added to a NSTableCellView are incorrectly positioned (see screenshot). But why? 
This is what I am trying to do. The objectValue set to my custom NSTableCellView has a variable number of data groups. Each data group can be represented by a standard NSControls (checkbox, popupbutton, etc).
What I did: I have the main xib file with the NSTableView and a xib file with a subview that represents one data group. The NSTableview is configured with a custom NSView.
This is my code
The background color is an instance variable of the view.
The orange diagonal line shows I don't have flipped coordinates.
The actual content of the subview will differ, but these elements (e.g. NSColorWell) have a well defined border and background. Makes it easier to see what is located where.
Ok, I could play around with setFrameOrigin until it's positioned correctly and do this trial and error for the first x number of subviews. But I would just end up with magic numbers. The origin of the subview within the superview is set to (0,0) and the coordinate system of the sub view is the lower left (see orange diagonal line). And then the NSTableCellView subclass wouldn't work outside of NSTableView.
Alternatively, I could try to make my own NSControl : NSView and draw different types of cells in the correct position.
The positioning is correct in both the view and tableview. However, it doesn't respond to mouse events. So this means implementing all the mouse tracking and clicking which is a lot of duplicate work. This will probably just end up as badly implemented standard NSControls like NSButton or NSPopupButton.
Any advice on how to proceed is appreciated!
This is what I am trying to do. The objectValue set to my custom NSTableCellView has a variable number of data groups. Each data group can be represented by a standard NSControls (checkbox, popupbutton, etc).
What I did: I have the main xib file with the NSTableView and a xib file with a subview that represents one data group. The NSTableview is configured with a custom NSView.
This is my code
Code:
-(int) heightValue
{
//adapt the height based on the number of data groups: x times the height of a subview + spacing
//do not return 0 !!!
// return [self frame].size.height;
if ([self objectValue])
{
// NSLog(@"d %@",[[self objectValue] description]);
return 50+10*[[self objectValue] intValue];//test value
}
return 16; //default height
}
-(void) setObjectValue:(id)objectValue
{
[super setObjectValue:objectValue];
//determine number of required subviews
....
//get a subview (has its own nib file)
NSNib *viewNib = [[NSNib alloc] initWithNibNamed:@"CellView" bundle:nil];
[viewNib instantiateNibWithOwner:self topLevelObjects:nil];
[viewNib release];
NSView *aSubView = [[[self mySubView] retain] autorelease];
[self setMySubView:nil];
//add subview
[self addSubview:aSubView];
...
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
for (NSView *subview in [self subviews])
{
[subview setFrameOrigin:NSMakePoint(0, 0)]; //placed here or in setObjectValue makes no difference
}
[[self myColor] set];
NSRectFill(dirtyRect);
NSRect rect = [self frame];
[[NSColor orangeColor] set];
NSBezierPath *path = [[NSBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 0)];
[path lineToPoint:CGPointMake(rect.size.width, rect.size.height)];
[path setLineWidth:3];
[path stroke];
}
The background color is an instance variable of the view.
The orange diagonal line shows I don't have flipped coordinates.
The actual content of the subview will differ, but these elements (e.g. NSColorWell) have a well defined border and background. Makes it easier to see what is located where.
Ok, I could play around with setFrameOrigin until it's positioned correctly and do this trial and error for the first x number of subviews. But I would just end up with magic numbers. The origin of the subview within the superview is set to (0,0) and the coordinate system of the sub view is the lower left (see orange diagonal line). And then the NSTableCellView subclass wouldn't work outside of NSTableView.
Alternatively, I could try to make my own NSControl : NSView and draw different types of cells in the correct position.
Code:
NSPopUpButtonCell *popupCell = [[NSPopUpButtonCell alloc] initTextCell:@"test" pullsDown:YES];
[popupCell drawWithFrame:NSMakeRect(0, 0, 99, 22)
inView:self];
Any advice on how to proceed is appreciated!
Attachments
Last edited: