Search code examples
objective-cnstablecellview

NSTableCellView *result change the text color according to the logic


i have a custom class "size" as object for a RestFull call. The result populates an array controlled by a NSArrayController. Then the TableView will bound to this controller in IB. All work as apected. What i would achieve is change color in NSViewCell based on sizes. Ex: Size "M" will be Red, "S" will be green, and "XXL" Brown.

.h

@interface RecordSize : NSObject  
@property (readwrite, retain) NSString *key;
@property (readwrite, retain) NSString *size;
-(id)initWithName:(NSDictionary *)row;
@end

.m

#import "RecordSize.h"

@implementation RecordSize
@synthesize key = _key, size = _size;

-(id)initWithName:(NSDictionary *)row {  
    self = [super init];
    if (self) {       
        _key = [row valueForKey:@"id"];
        _size = [row valueForKey:@"text"];       
    }
    return self;    
}
@end

delegate class constructor:

- (id)init {

        if (self) {
            self  = [super init];

            //*********** TableViev
            NSString * urlString = @"http://xxxxx/restfull/size/";

            RestateC *restSize = [[RestateC alloc]initWithName:urlString];
            //add Delegate

            restSize.delegate = self;

            NSArray*  aTmp = [restSize syncronize];
            NSMutableArray *thingSize = [[NSMutableArray alloc]init];

            for (NSDictionary *row in aTmp)
            {
                RecordSize *item = [[RecordSize alloc] initWithName:row];
                [thingsSize addObject: item ];
            }
            self.aRecordSize = thingsRecordSize;

            _sizeTableView.delegate = self;
        }
        return self;
}

method fro NSTablecolumn in a view based cell

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{

    NSTableCellView *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
       if( [[tableColumn identifier] isEqual:@"AGONIA"] ) {
           NSLog(@"IDENTITY %@", [tableColumn identifier]);

        if ([[result.textField stringValue] isEqualToString :@"M"]) {
            result.textField.textColor = [NSColor redColor];
        }

        if ([[result.textField stringValue] isEqualToString :@"S"]) {
            result.textField.textColor = [NSColor greenColor];
        }

         if ([[result.textField stringValue] isEqualToString :@"XXL"]) {
            result.textField.textColor = [NSColor brownColor];
        }

    }
    else  result.textField.textColor = [NSColor blackColor];

    return result;
}

so the method was call correctly from delegate but the logic does not work.

Any help is largely wellcome.


Solution

  • So the solution was arrived. The mistake initially come from search the values in NSTableCellView *result. The results don't contain any values: they are there only for display the values.

    finally:

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
        NSTableCellView *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
    
        if( [[tableColumn identifier] isEqual:@"SizeColumn"] ) {
    
            if ([[[_arrayInstance valueForKeyPath:@"sizes"]objectAtIndex:row] isEqualToString:@"M"]) {
                NSLog(@"FIND: %@", [[_arrayInstance valueForKeyPath:@"sizes"]objectAtIndex:row]);
                [result.textField setTextColor:[NSColor blueColor]];
                return result;
            }
            if ([[[_arrayInstance valueForKeyPath:@"sizes"]objectAtIndex:row] isEqualToString:@"XXL"]) {
                NSLog(@"FIND: %@", [[_arrayInstance valueForKeyPath:@"testo"]objectAtIndex:row]);
                [result.textField setTextColor:[NSColor redColor]];
                return result;
            }
    
        }
    
        return result;
        }