Re: How do I bind custom view properties to an NSCollectionViewItem?
Re: How do I bind custom view properties to an NSCollectionViewItem?
- Subject: Re: How do I bind custom view properties to an NSCollectionViewItem?
- From: Rick Mann <email@hidden>
- Date: Sun, 3 Jan 2010 12:16:16 -0800
On Jan 3, 2010, at 06:32:33, Markus Spoettl wrote:
>
> On Jan 3, 2010, at 9:11 AM, Rick Mann wrote:
>
>> In the example for NSCollectionView, they show binding text labels to the collection view item's represented object's properties. This is fine and makes sense.
>>
>> But my item view contains a custom view that needs access to the represented object in the NSCollectionViewItem. Since my custom view can't be bound within IB, I need to bind it programmatically. But I can't find the right place to do this. I tried doing it in awakeFromNib, with an outlet in my custom view to the NSCollectionViewItem, but it's sometimes nil (Currently I have data for two items, and the first time awakeFromNib is called, the item outlet is set, and the second time, it's not; not sure why).
>>
>> When does IB actually set the bindings specified in the nib? When can I add bind: calls and have access to both my view instance and the NSCollectionViewItem?
>
>
> What you could do is sub-class the NSCollectionViewItem your collection view is using (you will set the NSCollectionViewItem's class in IB) and implement -setRepresentedObject: which is called the at the time it is created and assigned its corresponding object.
>
> There you can access the view and represented object and do whatever you like with it.
>
> - (void)setRepresentedObject:(id)object
> {
> [super setRepresentedObject:object];
>
> MyView *myView = (MyView *)[self view];
> // bind view properties to object here
> }
>
> An alternative would be sub-classing the NSCollectionView and implementing -newItemForRepresentedObject: where you too can get access to the ItemView and view for the object.
Thanks, Markus, I took the first approach you suggested. I had tried overriding setView:, but I realize now why that wouldn't work.
However, it seems that the connections made in the template view in IB are not preserved in the copies made by NSCollectionView. This certainly limits the convenience of using IB, and I consider it a bug, but I wanted some confirmation before writing it up. Is it?
Thanks!
--
Rick
_______________________________________________
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