Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTableView Changing Text Color for a row

I need to change following properties for my NSTable View 1 -- Change Color:Row Color and Text Color when its selected 2 -- Change the Text Color , for each row it depends upon some input parameter,

For changing textcolor for each row, i should override delegate method willDisplayCell, This is what i have done , till now,

-- Creating the table ----

pMyTableView       = [[[CustomTableView alloc] initWithFrame:clipViewBounds] autorelease];


NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];

[firstColumn setWidth:35];

[pMyTableView  addTableColumn:firstColumn];

NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];

[secondColumn setWidth:180];

[pMyTableView  addTableColumn:secondColumn];
    [pMyTableView setRowHeight:30];

    [self SetContactTableDisplayAttribute];

[pMyTableView setDataSource:self];
[scrollView setDocumentView:pOnLineCTView];

    [pMyTableView setDelegate:self]

;

--- Other delegate Method -------------

- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    if([pColName isEqualToString:@"secondColumn"]) 
    {
           // Here there is some logic , to get the proper string that i wanted to display
        return @"tempString";

    }

}

---- Now this is how i am setting the text color ---

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {

    NSString *colName = [aTableColumn identifier];
    if([colName isEqualToString:@"secondColumn"]){
        NSTextFieldCell *pCell = aCell;
        [pCell setTextColor:[NSColor blueColor]];
    }

}

With the above code, its going to exception in the Log, i could see the line -[NSCell setTextColor:]: unrecognized selector sent to instance Looks like somewhere i need to set the text Field cell, but how and where i have no idea, kindly help me,

Another thing is, Initially i don't need any background for cell, but once when cell is selected , then also i might need to change the Background or you can say highlight color, can i get the same in WillDIsplayCell too

like image 520
Amitg2k12 Avatar asked Jan 25 '11 11:01

Amitg2k12


1 Answers

It has been a while since I have done this but I always refer to this blog post by Corbin Dunn when I need to do it: Cocoa: willDisplayCell delegate method of NSTableView, [NSCell setTextColor], and “source lists”

By the way, Corbin works at Apple and from what I understand is responsible for NSTableView. When he blogs about anything Cocoa I always be sure to bookmark it.

like image 128
sosborn Avatar answered Nov 04 '22 05:11

sosborn