Welcome, Guest
Username Password: Remember me

Allowing NSPredicates in TableView Sections
(1 viewing) (1) Guest

TOPIC: Allowing NSPredicates in TableView Sections

Allowing NSPredicates in TableView Sections 1 year, 7 months ago #1

Are there any plans to allow predicates in SCTableViewSections? I'd like to do something like:

 
 
NSPredicate *predicate = nil;
predicate = [NSComparisonPredicate predicateWithLeftExpression:...
 
tableViewModel = [[SCTableViewModel alloc] initWithTableView:self.tableView withViewController:self];
 
SCClassDefinition *sheetDef = [SCClassDefinition definitionWithEntityName:...
withManagedObjectContext:...withPropertyNames:...usingPredicate:predicate];
 
 
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4
Last Edit: 1 year, 7 months ago by panamind.

Re: Allowing NSPredicates in TableView Sections 1 year, 7 months ago #2

Actually I can create a category on SCTableViewSection:

 
 
- (id)initWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:predicate
{
// Create the sectionItems array
NSMutableArray *sectionItems = nil;
 
if(classDefinition.entity)
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:classDefinition.entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:classDefinition.keyPropertyName
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
 
[fetchRequest setPredicate:predicate];
 
sectionItems = [NSMutableArray arrayWithArray:[classDefinition.managedObjectContext
executeFetchRequest:fetchRequest
error:NULL]];
 
[sortDescriptor release];
[sortDescriptors release];
[fetchRequest release];
}
 
return [self initWithHeaderTitle:sectionHeaderTitle withItems:sectionItems
withClassDefinition:classDefinition];
}
 
 
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4

Re: Allowing NSPredicates in TableView Sections 1 year, 7 months ago #3

Thanks a lot Gary for suggesting this, we will definitely add it!
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Allowing NSPredicates in TableView Sections 1 year, 7 months ago #4

I added this to my copy of Sensible TableView.

I replaced the occurrences of
+ (id)sectionWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition

and
- (id)initWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate *)predicate;

with
+ (id)sectionWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate *)predicate;

and
- (id)initWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate *)predicate;

and implemented these methods in the implementation file.

Tarek, are you planning to add this to the next release of STV?
  • Patrick
  • OFFLINE
  • Senior Boarder
  • Posts: 49
  • Karma: 3

Re: Allowing NSPredicates in TableView Sections 1 year, 7 months ago #5

Instead of modifying Tarek's code, my solution was to create a category on SCTableViewSection:

SCTableViewSection+predicate.h

 
#import <Foundation/Foundation.h>
#import "SCTableViewSection.h"
 
@interface SCTableViewSection (helper)
 
+ (id)sectionWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate*)predicate;
 
 
- (id)initWithHeaderTitle:(NSString *)sectionHeaderTitle withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate*)predicate;
 
 
@end
 


SCTableViewSection+predicate.m

 
#import "SCTableViewSection+predicate.h"
 
 
@implementation SCTableViewSection (helper)
 
+ (id)sectionWithHeaderTitle:(NSString *)sectionHeaderTitle
withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate*)predicate
{
return [[[[self class] alloc] initWithHeaderTitle:sectionHeaderTitle
withEntityClassDefinition:classDefinition
usingPredicate:predicate] autorelease];
}
 
- (id)initWithHeaderTitle:(NSString *)sectionHeaderTitle withEntityClassDefinition:(SCClassDefinition *)classDefinition
usingPredicate:(NSPredicate*)predicate
{
// Create the sectionItems array
NSMutableArray *sectionItems = nil;
 
if(classDefinition.entity)
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:classDefinition.entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:classDefinition.keyPropertyName
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
 
[fetchRequest setPredicate:predicate];
 
sectionItems = [NSMutableArray arrayWithArray:[classDefinition.managedObjectContext
executeFetchRequest:fetchRequest
error:NULL]];
 
[sortDescriptor release];
[sortDescriptors release];
[fetchRequest release];
}
 
return [self initWithHeaderTitle:sectionHeaderTitle withItems:sectionItems
withClassDefinition:classDefinition];
}
 
@end
 


This way I don't have to alter the SCTableViewSection and can keep it current.
Just add #import SCTableViewSection+predicate.h to your view controller.
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4
Last Edit: 1 year, 7 months ago by panamind.

Re: Allowing NSPredicates in TableView Sections 1 year, 7 months ago #6

Thanks a lot for posting your code guys!

We are definitely planning to add this to our next release, but we're studying a more generic approach using an NSFetchRequest instead of NSPredicate. Until we do that, I think Gary's work of adding it as a category is awesome!
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72
Time to create page: 2.16 seconds