 |
 |
NSTableView format
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
Writing my small app I realized that I have a small problem.
I have data in a NSDictionary which I make in to NSArray. This NSArray is used to draw the contents of NSTableView. However, there's this one thing I couldn't do.
One column is numbers (well, they are there "as string" and this is creating the problem) and I want to display that column as formated column. Meaning, I'd like to set a format (from within IB) so that negative numbers would be RED.
If I do that, data is not presented in that column!?!?!?!?!?
Why?
And how can I make it work...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
What exactly are you doing in IB? I didn't think you could drag formatters to table columns.
Anyway, here's one way to get it to work:
Code:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
static NSNumberFormatter NumberFormatter = nil;
if (!*NumberFormatter)
{
// create the formatter
}
[aCell setFormatter:NumberFormatter];
}
Just register as your table view's delegate.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
Originally posted by Ibson:
What exactly are you doing in IB? I didn't think you could drag formatters to table columns.
Well, YOU CAN drag formaters... but maybe you are right that they don't really work.
This is what I tried:
-Double click on the Table
-Click on the column [title?]
-Drag the formater:
---Date, or
---Number
-set it up
Unfortunately it didn't automatically display the strings as formated numbers...
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
Look what they do here!
http://www05.u-page.so-net.ne.jp/xd5...TableView.html
[The explanations are in Japanese or something... I don't understand it, but the examples are good: IN ENGLISH  ]
It kind of a works. Meaning, I can set the precision from with in IB [how many digits after `.' will be displayed].
However, I can't get the negative number to go RED!!!!!!!!!!!!!!
Does anyone have an idea why??
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
You're going about it the hard way. Forget the formatters and do it yourself.
The simplest solution is to return what you want displayed from your datasource method tableView:objectValueForTableColumn:row:...
Code:
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSDictionary *red = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSColor redColor], NSForegroundColorAttributeName, nil];
NSAttributedString *attedString;
if ([yourString intValue] <= -1) {
attedString = [[NSAttributedString alloc]
initWithString:yourString
attributes:red];
}
[red release];
if (attedString != nil)
return [attedString autorelease];
return yourString;
}
HTH!
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
The nice thing about the IB formaters is that I can define "0" as, say, "Empty" or "CORRECT" or... whatever...
AND it can be easily translated to other languages for "localization" purposes.
If I write in into the code... well... I don't want to define anything in Japanese if you understand me. It's not that I don't like Japanese or something... it's just that I have a certain limitations in number of languages I speak 
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
Yes, that's true but it seems that you could just as easily get a localized color for "Correct" (/whatever) and use that as your foreground in the attributed string...
Far be it from me to offer alts. though. We wouldn't want too many ways to do things! 
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
Originally posted by IamBob:
Yes, that's true but it seems that you could just as easily get a localized color for "Correct" (/whatever) and use that as your foreground in the attributed string...
Far be it from me to offer alts. though. We wouldn't want too many ways to do things!
I will try your method... let's see if it will work with localizations and all.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
If you've already got a working solution I wouldn't bother. But you never said you found a solution so I dunno..
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
Originally posted by IamBob:
If you've already got a working solution I wouldn't bother. But you never said you found a solution so I dunno..
It was working fine except for the color.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
Hmm...want something else to try?
Create an outlet that connects your formatter and some object. In the awakeFromNib of "some object":
Code:
- (void)awakeFromNib
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
// I'm quessing this could be a localized color
// though I'm not sure how you'd do that
[attrs setObject:[NSColor redColor] forKey:@"NSColor"];
[yourFormatter setTextAttributesForNegativeValues:attrs];
}
HTH! 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|