Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTableView format

NSTableView format
Thread Tools
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 5, 2002, 05:38 AM
 
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
Reply With Quote
Sep 5, 2002, 06:00 AM
 
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.
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 5, 2002, 06:14 AM
 
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...
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 5, 2002, 07:44 AM
 
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
Reply With Quote
Sep 9, 2002, 01:16 PM
 
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!
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 9, 2002, 06:02 PM
 
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
Reply With Quote
Sep 9, 2002, 07:25 PM
 
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!
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 10, 2002, 05:09 AM
 
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
Reply With Quote
Sep 10, 2002, 09:56 AM
 
If you've already got a working solution I wouldn't bother. But you never said you found a solution so I dunno..
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 11, 2002, 05:41 AM
 
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
Reply With Quote
Sep 11, 2002, 11:22 AM
 
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!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2