Re: NSDateFormatter and NSCell oddness
Re: NSDateFormatter and NSCell oddness
- Subject: Re: NSDateFormatter and NSCell oddness
- From: Nathan Kinsinger <email@hidden>
- Date: Wed, 9 Jul 2008 04:25:15 -0600
On Jul 8, 2008, at 6:43 PM, email@hidden wrote:
Hello,
I'm trying to replicate the Finder's behaviour for date fields when
resizing. I found this on the archives which pretty much has the
answer:
http://www.cocoabuilder.com/archive/message/cocoa/2005/8/9/143911
I'm trying to update it so that it returns the correct value for the
current localisation. This is the full code of my NSCell subclass
(debugging commented out):
@implementation DMSquishableDateCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]
autorelease];
[dateFormatter
setFormatterBehavior:NSDateFormatterBehavior10_4]; // needed?
int width = cellFrame.size.width;
if (width < 130)
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
else if (width < 145)
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
else // if (width < 170)
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[self setFormatter:dateFormatter];
[super drawWithFrame:cellFrame inView:controlView];
//NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:
118800];
//NSLog(@"formatted date: %@", [dateFormatter stringFromDate:date]);
// if ([self hasValidObjectValue])
// NSLog(@"formatted date: %@", [dateFormatter stringFromDate:
[self objectValue]]);
}
@end
In my awakeFromNib: method where the table is created, I set the
cell thusly:
DMSquishableDateCell *dateCell = [[[DMSquishableDateCell alloc]
init] autorelease];
NSTableColumn *creationDateColumn = [table
tableColumnWithIdentifier:@"creation_date"];
[creationDateColumn setDataCell:dateCell];
However, the column is completely blank. If I remove the three lines
above, the dates are properly displayed. The lines that I've
commented above verify that the formatter itself is working
properly. The cell objects come straight from CoreData, and seem to
be objects of class "__NSCFDate".
What am I doing wrong?!?
Thanks!
Demitri
The problem you are having is that NSCell doesn't know how to draw
text (or anything else for that matter). In your subclass you are not
drawing the date string yourself so it is blank. When you delete the
lines adding your cell class as the data cell then the cell is using
the default NSTextFieldCell which does draw text.
There is a sample project from apple that has a date cell that changes
based on the width of the cell:
http://developer.apple.com/samplecode/PhotoSearch/index.html
-- Nathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden