我有一个自定义类“大小”作为 RestFull 调用的对象。结果填充了一个由 NSArrayController 控制的数组。然后 TableView 将绑定到 IB 中的这个控制器。所有工作都按预期进行。我要实现的是根据大小更改 NSViewCell 中的颜色。例如:“M”号为红色,“S”号为绿色,“XXL”号为棕色。
。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
委托类构造函数:
- (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;
}
基于视图的单元格中的 NSTablecolumn 方法
- (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;
}
所以该方法是从委托正确调用的,但逻辑不起作用。
任何帮助在很大程度上都是受欢迎的。