Trouble filtering bound table view
Trouble filtering bound table view
- Subject: Trouble filtering bound table view
- From: Daniel Wambold <email@hidden>
- Date: Thu, 02 Aug 2007 18:26:01 -0400
OK, I've read the article entitled, "Filtering Using a Custom Array
Controller" in the Cocoa Bindings Programming Topics. However, I have
failed to succeed in implementing the sort of filtering I would like.
I have an NSPersistentDocument app (using CoreData and bindings) that
has a window, two NSTableView objects (one column each), two
subclassed NSArrayControllers, and a detail view for one. When a user
chooses a "client" from the left table view (no selection is NOT
allowed), the "billableEvents" in the right table will show that
client's events. The sort box should limit those events to ones
containing the search string in either the myNotes or myEvent keys.
Regardless of the search string (including nil), the result should
never show an event not belonging to that client, and should only
show items with a myPaidFlag set to true when the user defaults say
so. Despite the following code, the interface is buggy, with down-
arrowing leading to occasional jumps in the location of the selected
row. The selectionIndex (as observed in NSLog outputs) occasionally
has a very large integer rather than something like 0 through 10).
Any suggestions would be greatly appreciated! The interface looked
great when I thought I didn't have to implement my own filtering, but
as soon as I checked the "hide paid items" preference, the list index
became nonlinear and the program crashed. It doesn't crash now, but
it's not right either.
Thanks for your insight!
-Dan
(The open-source code for the previous version is available at http://
www.ascendiac.com/macosx.html if you want to see the rest.)
(code from my subclassed NSArrayController for the BillableEvent object)
- (NSArray *)arrangeObjects:(NSArray *)objects
{
NSMutableArray *mySearchResults = [[[NSMutableArray alloc]
initWithCapacity:[objects count]] autorelease];
NSManagedObjectContext *myMOC = [self managedObjectContext];
NSManagedObjectModel *myMOCModel = [myMyDocumentOutlet myMOCModel];
NSFetchRequest *myEntityFetchRequest = [[[NSFetchRequest alloc]
init] autorelease];
NSString *myClientName = [[myClientControllerOutlet selection]
valueForKey:@"myClientName"];
NSEntityDescription *myEventEntity = [[myMOCModel entitiesByName]
valueForKey:@"BillableEvent"];
[myEntityFetchRequest setEntity: myEventEntity];
//////
// The search predicate must change based on the user preferences
regarding showing paid items in the list:
//////
if (([[[NSUserDefaults standardUserDefaults]
objectForKey:myHidePaidItemsKey] intValue]) == 1) // User wants to
HIDE paid items, so search for non-paid items only.
{
if (mySearchString == nil)
{
[myEntityFetchRequest setPredicate: [NSPredicate
predicateWithFormat:@"myPaidFlag == NO &&
myClientPointer.myClientName == %@ ", myClientName]];
}
else
{
[myEntityFetchRequest setPredicate: [NSPredicate
predicateWithFormat:@"(%@ IN[cd] myEvent || %@ IN[cd] myNotes) &&
myPaidFlag == NO && myClientPointer.myClientName == %@",
mySearchString, mySearchString, myClientName]];
}
}
else
{
if (mySearchString == nil)
{
[myEntityFetchRequest setPredicate: [NSPredicate
predicateWithFormat:@"myClientPointer.myClientName == %@ ",
myClientName]];
}
else
{
[myEntityFetchRequest setPredicate: [NSPredicate
predicateWithFormat:@"(%@ IN[cd] myEvent || %@ IN[cd] myNotes) &&
myClientPointer.myClientName == %@", mySearchString, mySearchString,
myClientName]];
}
}
[myEntityFetchRequest setSortDescriptors:[NSArray arrayWithObject:
[[[NSSortDescriptor alloc] initWithKey:@"myEvent" ascending:YES]
autorelease]]];
NSError *myError = nil;
mySearchResults = [[myMOC executeFetchRequest:myEntityFetchRequest
error:&myError] mutableCopy];
if (nil != myError)
{
// If the fetch request fails, return everyting?
mySearchResults = [objects mutableCopy];
}
return [super arrangeObjects:mySearchResults];
}
_______________________________________________
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