Re: @distinctUnionOfObjects and NSTableView
Re: @distinctUnionOfObjects and NSTableView
- Subject: Re: @distinctUnionOfObjects and NSTableView
- From: mmalcolm crawford <email@hidden>
- Date: Sat, 28 May 2005 21:26:50 -0700
On May 28, 2005, at 7:16 AM, Fletcher T. Penney wrote:
I would like to take an NSArrayController that is created from Core
Data and use the arrangedObject's (I do a lot of nested predicate
building to display only the desired values) to build a list of
distinct properties to be placed in a single column table.
Instantiate an array controller ("NamesController") that manages an
array of strings. Bind its contentArray to:
[PeopleArrayController]email@hiddenNam
e
To your window, add a table view with a single column. Bind the
table column's value to
[NamesController].arrangedObjects
Instantiate another array controller ("Filtered Persons") that
manages an array of Persons. Bind its contentArray to:
[PeopleArrayController].arrangedObjects
Bind its managedObjectContext to:
[PeopleArrayController].managedObjectContext
In your NSDocument subclass, add the document as an observer of the
NamesController's selection:
- (void)windowControllerDidLoadNib:(NSWindowController *)
windowController
{
[super windowControllerDidLoadNib:windowController];
[namesArrayController addObserver:self forKeyPath:@"selection"
options:nil context:SelectedNameContext];
}
Then implement a suitable observe... method to update the predicate
on the Filtered Persons array controller:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
if (context == SelectedNameContext) {
NSArray *selectedNames = [namesArrayController
selectedObjects];
if ([selectedNames count] == 0) {
[filteredPersonsArrayController setFilterPredicate:nil];
}
else {
NSString *selectedName = [selectedNames objectAtIndex:0];
NSPredicate *newPredicate = [NSPredicate
predicateWithFormat:
@"firstName like %@", selectedName];
[filteredPersonsArrayController
setFilterPredicate:newPredicate];
}
[filteredPersonsArrayController rearrangeObjects];
return;
}
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
Then tidy up:
- (BOOL)windowShouldClose:(id)sender
{
[namesArrayController removeObserver:self
forKeyPath:@"selectedObjects"];
return YES;
}
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden