Re: Key path help for complex binding
Re: Key path help for complex binding
- Subject: Re: Key path help for complex binding
- From: David Hirsch <email@hidden>
- Date: Tue, 01 Dec 2009 21:55:03 -0800
Well, I found a work-around. Bindings still fail to work, but I can
trigger a KVO message to be sent to the NSTableColumn manually by
calling rearrangeObjects on my intermediary NSArrayController
(selectedCourseRoomCosts).
I'd still like to make bindings do this without the kludge.
-Dave
On Dec 1, 2009, at 4:19 PM, David Hirsch wrote:
First thanks for all the help to date. I'm learning a great deal.
The current problem:
I have a model for which I cannot seem to figure out the correct key
paths for binding.
My doc has a rooms array (of Rooms) and a courses array (with
NSArrayControllers). Each course has a roomConstraint, which
contains a roomCosts array of RoomCosts each of which has a Room*,
"room" (points to an element of the doc's rooms array) and a
"cost" (float).
I can easily bind a table column to the rooms array. I am trying to
bind two table columns to the RoomCosts data fields (each Room is
basically a NSMutableString for now). They change depending on the
currently selected course in a separate tableview.
Earlier, I could make this work in a kludgy way, by creating an
intermediary NSArrayController (selectedCourseRoomCosts) whose
contentArray was bound to
courseController.selection.roomConstraint.roomCosts, and then
binding the table columns to selectedCourseRoomCosts.arrangedObjects.room.name
and selectedCourseRoomCosts.arrangedObjects.cost
When creating each Course object, I would copy the rooms array into
the roomConstraint. Fine. I have no problem keeping that array in
sync with the master rooms array.
The problem is, when I add a new Room to the master rooms array, and
then add that room to the roomCosts array of each Course's
roomConstraint, the table column doesn't get updated, until I tweak
it manually (by adding a new course). It is still bound to the
existing items in the rooms array - if I alter their name, that get
observed and reflected in the table, but adding new ones does not
get observed.
Here is some of the relevant code (note that everything is shown
here as an ivar, but they are all properties as well):
@interface ClassSchedulerDoc : NSDocument
{
IBOutlet NSArrayController *courseController;
IBOutlet NSArrayController *roomController;
NSMutableArray *rooms;
NSMutableArray *courses;
}
@implementation RoomArrayController
- (void) addObject:(id) newObject {
[snip]
// Add the object to the rooms array of each course
[courseController addRoom:newObject];
}
@implementation CourseArrayController
- (void) addRoom:(Room *) newRoom {
for (Course *curCourse in [self arrangedObjects]) {
[curCourse addRoom:newRoom];
}
}
@interface CourseRoomConstraint : Constraint {
NSMutableArray * roomCosts; // array of RoomCosts
}
@implementation CourseRoomConstraint
- (void) addRoom:(Room *) newRoom {
[roomCosts addObject:[[RoomCost alloc] initWithRoom:newRoom]];
}
@interface RoomCost : NSObject <NSCoding> {
Room *room; // this is a pointer to the real object in the real
Rooms array - a weak reference
float cost;
}
Thanks in advance,
Dave
_______________________________________________
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
============================
Dave Hirsch
Associate Professor
Department of Geology
Western Washington University
persistent email: email@hidden
http://www.davehirsch.com
voice: (360) 389-3583
aim: email@hidden
vCard: http://almandine.geol.wwu.edu/~dave/personal/DaveHirsch.vcf
============================
_______________________________________________
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