Hi again,
Trying to set a predicate on the SCArrayOfObjectsModel. The examples I've found use
tableViewModelWithTableView:withViewController:withEntityClassDefinition:usingPredicate:
but I can't use it. The closest i've found is modelWithTableView. When I use it I, I get the full list (not filtered). I can't see what I'm doing wrong. Here's the code I'm using:
SCEntityDefinition *entriesDef =[SCEntityDefinition definitionWithEntityName:@"WOSession" managedObjectContext:context propertyNamesString:@"sessionDate"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionDate == %@",[selDate valueForKey:@"sessionDate"]];
SCArrayOfObjectsModel *exModel = [SCArrayOfObjectsModel modelWithTableView:self.tableView entityDefinition:entriesDef filterPredicate:predicate];
exModel.itemsAccessoryType=UITableViewCellAccessoryNone;
self.tableViewModel = exModel;
Thanks in advance for your help.

Setting predicate of SCArrayOfObjectsModel in SCViewController
Started by
Cesar Porras
, Jan 21 2013 07:50 PM
6 replies to this topic
#1
Posted 21 January 2013 - 07:50 PM
#2
Posted 23 January 2013 - 02:32 PM
Hi Cesar,
The code you pasted above should work. Are you sure your predicate is formatted correctly? (you can try a different simple predicate just to test that it's working).
The code you pasted above should work. Are you sure your predicate is formatted correctly? (you can try a different simple predicate just to test that it's working).
#3
Posted 24 January 2013 - 06:26 PM
Hi,
I tried a simple predicate with an SCTableViewController. That worked the way I expected (filtered).
I tried the same predicate with an SCViewController and an SCArrayOfObjectsModel and I getting the full list (not filtered).
The reason I'm using the SCViewController is that I need to add a toolbar and bar button items. I'm able to add those with an SCViewController and not on the SCTableViewController. Maybe my approach is wrong.
I tried a simple predicate with an SCTableViewController. That worked the way I expected (filtered).
I tried the same predicate with an SCViewController and an SCArrayOfObjectsModel and I getting the full list (not filtered).
The reason I'm using the SCViewController is that I need to add a toolbar and bar button items. I'm able to add those with an SCViewController and not on the SCTableViewController. Maybe my approach is wrong.
#4
Posted 24 January 2013 - 07:08 PM
You approach is actually perfectly correct. Would you please post all the code in your view controller? Thanks.
#5
Posted 24 January 2013 - 08:01 PM
#import "ExercisesViewController.h"
@interface ExercisesViewController ()
@end
@implementation ExercisesViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSManagedObjectContext *context = [(id)[UIApplication
sharedApplication].delegate managedObjectContext];
if (!context) {
NSLog(@"There is an error!!!");
}
if (context == nil) {
NSLog(@"Context is nil in UserViewController");
}
else {
NSLog(@"Context is set in UserViewController");
}
self.navigationBarType = SCNavigationBarTypeAddEditRight;
//Add theme
self.tableViewModel.theme = [SCTheme themeWithPath:@"Theme.sct"];
// Create the exercise definition
SCEntityDefinition *entriesDef =[SCEntityDefinition definitionWithEntityName:@"Exercises" managedObjectContext:context propertyNamesString:@"name;reps;weight;date"];
NSLog(@"Exercies for Predicate: %@", @"Bicep Curls");
//SCArrayOfObjectsModel *exModel = [SCArrayOfObjectsModel modelWithTableView:self.tableView entityDefinition:entriesDef];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"Bicep Curls"];
SCArrayOfObjectsModel *exModel = [SCArrayOfObjectsModel modelWithTableView:self.tableView entityDefinition:entriesDef filterPredicate:predicate];
exModel.addButtonItem = self.addButton;
[SCTableViewCell cellWithText:@"No Exercises yet!"
textAlignment: NSTextAlignmentCenter];
self.tableViewModel = exModel;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
=========================================================================================================================================
#import "ExercisesTableViewController.h"
@interface ExercisesTableViewController ()
@end
@implementation ExercisesTableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSManagedObjectContext *context = [(id)[UIApplication
sharedApplication].delegate managedObjectContext];
if (!context) {
NSLog(@"There is an error!!!");
}
if (context == nil) {
NSLog(@"Context is nil in UserViewController");
}
else {
NSLog(@"Context is set in UserViewController");
}
self.navigationBarType = SCNavigationBarTypeAddEditRight;
// Create the Exercise definition
SCEntityDefinition *exerDef =
[SCEntityDefinition definitionWithEntityName:@"Exercises"
managedObjectContext:context
propertyNamesString:@"name;reps;weight;date"];
SCPropertyDefinition *namePropertyDef = [exerDef
propertyDefinitionWithName:@"name"];
namePropertyDef.required = TRUE;
namePropertyDef.title = @"Exercise Name";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"Bicep Curls"];
// Create the the user section
SCArrayOfObjectsSection *exerSection =
[SCArrayOfObjectsSection sectionWithHeaderTitle:nil
entityDefinition:exerDef filterPredicate:predicate];
exerSection.addButtonItem = self.addButton;
exerSection.placeholderCell =
[SCTableViewCell cellWithText:@"No Exercises yet!"
textAlignment: NSTextAlignmentCenter];
[self.tableViewModel addSection:exerSection];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
=========================================================================================================================================
Entity: Exercises
Attributes: name,resp,weight,date
#6
Posted 27 January 2013 - 09:00 AM
As a work around, is it possible to add customized buttons (previous and next) tied to the filter on the right side of the navigation bar (in place of the + and edit buttons) in an SCTableViewController?
#7
Posted 06 February 2013 - 09:52 PM
Hi Cesar,
Please try replacing:
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"name == %@",@"Bicep Curls"];
With
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"name == '%@'",@"Bicep Curls"];
And let me know if it's working now.
Please try replacing:
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"name == %@",@"Bicep Curls"];
With
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"name == '%@'",@"Bicep Curls"];
And let me know if it's working now.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users