Is it possible to use cocoa bindings with NSTableView and Many-to-Many relationships

96 views Asked by At

I'd like to know is it possible to use cocoa bindings to show one-to-many or many-to-many relationships on NSTableView row.

What I'm trying to do is show one entity per row and in one column I'd like to show thru relationship more than one attributes from another entity.

Currently my schema is something like this:

Person <--->> tag

One row should look like this:

Name | Birth   | Address    | tag
--------------------------------------------------------
jon   75/12/13   123 street   handyman, contractor
paul  53/03/20   53 avenue 1  contractor, swimmer, biologist

is that even possible?

1

There are 1 answers

5
Ken Thomases On BEST ANSWER

There are a couple of ways you can do this. For purposes of discussion, I'll assume a collection property tags for the one-to-many relationship.

You can make a dependent property which is the tag list string:

+ (NSSet*)keyPathsForValuesAffectingTagList
{
    return [NSSet setWithObject:@"tags"];
}
- (NSString*) tagList
{
    return [[self.tags sortedArrayUsingSelector:@selector(localizedStandardCompare:)] componentsJoinedByString:@", "];
}

(If tags is a set rather than an array, you'd use self.tags.allObjects.)

Since this is sort of specific to how the tag list is presented in a view, it can be considered more part of the view layer than the model layer. Therefore, you might define these methods in a category on your class, rather than the class itself.

Another approach is to do the same thing using a value transformer class. You'd bind the text field to the tags collection property but specify the name of a custom value transformer class. That class would transform from the collection class (array or set) and produce a string in a manner similar to the above.

@interface TagListTransformer : NSValueTransformer {}
@end

@implementation TagListTransformer

+ (Class) transformedValueClass
{
    return [NSString class];
}

+ (BOOL) allowsReverseTransformation
{
    return NO;
}

- (id) transformedValue:(id)value
{
    return [[value sortedArrayUsingSelector:@selector(localizedStandardCompare:)] componentsJoinedByString:@", "];
}

@end