The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   Omni Lounge (http://forums.omnigroup.com/forumdisplay.php?f=24)
-   -   NSTableView Cell background image weird problem (http://forums.omnigroup.com/showthread.php?t=14090)

symadept 2009-10-06 06:38 PM

NSTableView Cell background image weird problem
 
I am trying to display Transparent image for Normal cell and the Opaque color, Green for the Selected cell.

I could manage to do this way. Normal case happens well and once selected Green colored image is shown. But once I select other one then the Green color image in the previous cell remains same. How can I resolve this.

I can't upload images thats why I am giving the details like this


Normal Normal Normal
Normal -> Select 2 -> Highlight -> Select 3 -> Highlight
Normal Normal Highlight

This is with only the problem for Transparent image. If it is opaque then no issue.

Here is my sample code
[CODE]
@implementation CustomTableView

- (void)awakeFromNib
{
NSLog(@"CustomTableView awakeFromNib");
[[self enclosingScrollView] setDrawsBackground:NO];
}

- (void)drawBackgroundInClipRect:(NSRect)clipRect
{
NSLog(@"CustomTableView drawBackgroundInClipRect");
}

#pragma mark -
#pragma mark Selection Highlighting

- (id)_highlightColorForCell:(NSCell *)cell
{
// we need to override this to return nil
// or we'll see the default selection rectangle when the app is running
// in any OS before leopard

// you can also return a color if you simply want to change the table's default selection color
return nil;
}

@end

@implementation CustomTableCell
- (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView *)theControlView
{
NSLog(@"CustomTableCell drawInteriorWithFrame");
NSImage *bgImage = [NSImage imageNamed:@"DefaultCell"];

// Make attributes for our strings
NSMutableParagraphStyle * aParagraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[aParagraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[aParagraphStyle setAlignment:NSCenterTextAlignment];



// Title attributes: system font, 14pt, black, truncate tail
NSMutableDictionary * aTitleAttributes = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSColor blackColor],NSForegroundColorAttributeName,
[NSFont systemFontOfSize:21.0],NSFontAttributeName,
aParagraphStyle, NSParagraphStyleAttributeName,
nil] autorelease];

// Make a Title string
NSString * aTitle = [self stringValue];//[NSString stringWithString:@"Title"];
// get the size of the string for layout
//NSSize aTitleSize = [aTitle sizeWithAttributes:aTitleAttributes];

// Icon box: center the icon vertically inside of the inset rect
/*
NSRect anIconBox = NSMakeRect(anInsetRect.origin.x,
anInsetRect.origin.y + anInsetRect.size.height*.5 - anIconSize.height*.5,
anIconSize.width,
anIconSize.height);*/
NSLog(@"CellFrame:[%f %f] [%f %f]", theCellFrame.origin.x, theCellFrame.origin.y, theCellFrame.size.width, theCellFrame.size.height);
NSRect anIconBox = theCellFrame;
NSRect aTitleBox = NSMakeRect(theCellFrame.origin.x,
theCellFrame.origin.y + theCellFrame.size.height/2-10,
theCellFrame.size.width,
theCellFrame.size.height);


if( [self isHighlighted])
{
// if the cell is highlighted, draw the text white
[aTitleAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
bgImage = [NSImage imageNamed:@"FocusedCell"];
}
else
{
// if the cell is not highlighted, draw the title black and the subtile gray
[aTitleAttributes setValue:[NSColor orangeColor] forKey:NSForegroundColorAttributeName];
}

// Draw the icon
[bgImage drawInRect:anIconBox fromRect:NSZeroRect operation:NSCompositePlusLighter fraction:1.0];

// Draw the text
[aTitle drawInRect:aTitleBox withAttributes:aTitleAttributes];





}
@end

@implementation TableViewController

- (id)init
{
NSLog(@"TableViewController init");
self = [super init];
if(self) {
mItems = [[NSArray arrayWithObjects:@"Value1", @"Value2", nil] retain];
}
return self;
}

- (void)awakeFromNib
{
NSLog(@"TableViewController awakeFromNib");
// set the custom cell as the table view's cell class
CustomTableCell * aCustomCell = [[[CustomTableCell alloc] init] autorelease];
[[mTableView tableColumnWithIdentifier:@"theTableColumn"] setDataCell:aCustomCell];
[mTableView setRowHeight:50.0f];
}

#pragma mark -
#pragma mark NSTableView Data Source Methods
- (int)numberOfRowsInTableView:(NSTableView *)theTableView
{
NSLog(@"Tableviewcontroller numberOfRowsInTableView");
return [mItems count];
}


- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSLog(@"Tableviewcontroller objectValueForTableColumn");
return [mItems objectAtIndex:row];
}

@end
[/CODE]


All times are GMT -8. The time now is 03:05 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.