Re: Custom NSCell and Bindings
Re: Custom NSCell and Bindings
- Subject: Re: Custom NSCell and Bindings
- From: Gustavo Pizano <email@hidden>
- Date: Tue, 28 Jun 2011 22:35:09 +0200
Hello Carter.
Right now I can't remember the name of the example.
Its a little tricky but its do-able.
your MO subclass must do something like:
-(NSDictionary *)userDictionary{
return [self dictionaryWithValuesForKeys:[NSArray arrayWithObjects:@"firstName",@"lastName",@"avatar",@"title",nil]];
}
+(NSSet *)keyPathsForValuesAffectingUserDictionary{
return [NSSet setWithObjects:@"firstName",@"lastName",@"avatar",@"title",nil];
}
which the right properties.
then in the xib bind the value of the tablecolum to arrangedObjects.userDictionary.
then subclass NSCell, on the awakeFromNib of the controller that contains the tableview tell the TableColum to set its dataCell as the instance of my custom cell, (of course I have to initialize my cell before that particular line). then in my cell what I do is:
//====================================================================================================================================================
//Draw the cell that contains the name of the user andits picutre
//This methods tales the objectValue which is a NSDictionary, all this is done bia bindings,
//the use entoty has a property NSDictionary keysDictionary which will return all the keys of its propertioes with its corresponding values see @ User.m
//====================================================================================================================================================
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
NSDictionary * cellValues = [self objectValue];
NSString * firstName = [cellValues valueForKey:@"firstName"];
NSString * lastName = [cellValues valueForKey:@"lastName"];
NSString * completeName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
NSPoint textPoint;
NSDictionary *textAttributes;
NSColor * fontColor;
fontColor = [self isHighlighted]?[NSColor whiteColor]:[NSColor blackColor];
textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: fontColor, NSForegroundColorAttributeName, [NSFont fontWithName:@"Optima" size:(CGFloat)13.0], NSFontAttributeName, nil];
NSSize nameSize = [completeName sizeWithAttributes:textAttributes];
textPoint.x = cellFrame.origin.x + 5.0f;
textPoint.y = cellFrame.origin.y +(cellFrame.size.height - nameSize.height)/2.0f - 5.0f ;
NSRect rectOfDraw = NSMakeRect(textPoint.x, textPoint.y,cellFrame.size.width - 50.0f,nameSize.height);
[completeName drawWithRect:rectOfDraw options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes];
//Getting the Avatr Value
NSData * avatar = [cellValues valueForKey:@"avatar"];
NSImage * avatarImage = [[[NSImage alloc] initWithData:avatar] autorelease];//[NSImage imageNamed:@"NoAvatar"];
[[NSGraphicsContext currentContext] saveGraphicsState];
CGFloat yOffset = cellFrame.origin.y;
//Checking if the controll view its flipped or no
//Most probably it is.
//then create an affine tarsnfor and translate its coordinates
if ([controlView isFlipped]) {
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy: cellFrame.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat];
yOffset = 0-cellFrame.origin.y;
}
//Create and interpolation
//medium its ok, due we don't need to show a high resolutiuon thumbnail
NSImageInterpolation interpolation = [[NSGraphicsContext currentContext] imageInterpolation];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationMedium];
//configure the image drawrect and size and pritn in on screen
if(avatarImage != nil){
NSRect imageRect ;
imageRect = NSZeroRect;
imageRect.size = [avatarImage size];
NSRect drawingRect;
drawingRect.origin.x = cellFrame.size.width - 40.0f;
drawingRect.origin.y = yOffset + 2.0;
drawingRect.size = NSMakeSize(37.0f, 37.0f);
[avatarImage drawInRect:drawingRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:0.9];
}
[[NSGraphicsContext currentContext] setImageInterpolation: interpolation];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
basically is jut taking the values from the cell objectvalue which is a dictionary and then is just set up of the things inside. So it displays correctly the name, the last name and the image of the user, in my case.
Note that this doesn't allow to edit the data inside the cell, for that you will need not to draw the strings but set another cell with the name, or something like that, im not sure.
I hope it helps
Gustavo
On Jun 28, 2011, at 10:11 PM, Sean McBride wrote:
> On Fri, 24 Jun 2011 09:33:38 -0400, Carter R. Harrison said:
>
>> It's a pretty typical situation. I've got my model objects stored in an
>> NSSet. An NSArrayController that is bound to the NSSet. And then an
>> NSTableView with one NSTableColumn bound to the arrangedObjects.name
>> property of the NSArrayController. It works great. Now I've introduced
>> a custom NSCell object into the NSTableView. The custom cell displays a
>> title, a subtitle, and a status image - all of which I want to be able
>> to bind to 3 separate properties of the model object. How do I do
>> this? Right now with the 'value' binding bound to the
>> arrangedObjects.name property, on the title of my custom cell updates
>> when the model updates. If the subtitle or status update in the model
>> object the cell does not update. Any help is greatly appreciated.
>
> Basically, you can't. :(
>
> The table column has only one value binding, so can only be bound to one thing, not many. You could change your model to have one property that is an amalgamation of your title, subtitle, and image and bind to that.
>
> --
> ____________________________________________________________
> Sean McBride, B. Eng email@hidden
> Rogue Research www.rogue-research.com
> Mac Software Developer Montréal, Québec, Canada
>
>
> _______________________________________________
>
> 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
_______________________________________________
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