Re: Questions about NSPopupButtonCell and NSBrowser
Re: Questions about NSPopupButtonCell and NSBrowser
- Subject: Re: Questions about NSPopupButtonCell and NSBrowser
- From: Graham Cox <email@hidden>
- Date: Fri, 05 Jun 2015 09:39:12 +1000
> On 5 Jun 2015, at 9:19 am, Frank D. Engel, Jr. <email@hidden> wrote:
>
> - (void)browser:(NSBrowser *)browser willDisplayCell:(MyBrowserCell *)cell atRow:(NSInteger)row column:(NSInteger)column
> {
> // Find the item and set the image.
> WhateverObject *c = [browser itemAtRow:row inColumn:column];
> [cell bind:@"image" toObject:c withKeyPath:@"icon" options:nil];
> }
This looks wrong to me.
If you have the source object (c) and the cell that is to display the image (cell), why not just set the image directly?
[cell setImage:[c icon]];
The binding might help when the “icon” property changes, but setting up the binding here (which is effectively within a call to -drawRect: of the NSBrowser) is almost certainly incorrect. The binding needs to be set up outside of the drawing pathway, but since cells are annoying things, just binding the cell’s image to the icon property won’t work - the cell won’t automatically refresh the relevant part of the browser view.
Cells generally don’t update their host views on a change, because they’re designed to be reusable in a lot of different circumstances - the view uses the cell to draw some content, but the cell is unaware of which view it belongs to so there isn’t a general way for a cell to update its host view. It’s also undesirable, because many classes such as NSTableView and NSBrowser set up the cell’s state (properties) on the fly just before the cell is drawn, so if the cell dirtied the view when that happened you’d get an infinite redraw cycle.
So what you need to do is to have a property or method on your browser controller that sets the relevant cell’s image property AND refreshes the correct part of the view, then bind that controller method to the object’s icon property.
—Graham
P.S. Cells in general are deprecated, so getting away from code that requires them is the way of the future.
_______________________________________________
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