View Single Post
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