UA-17470720-3

Jump to content


Photo
- - - - -

How to Apply Date Formatter to Main List

core-data

  • Please log in to reply
1 reply to this topic

#1 aaronium112

aaronium112

    Jr. Member

  • STV 3.0 Std
  • PipPip
  • 21 posts
Reputation: 7
Good

Posted 04 July 2013 - 03:52 PM

Hi guys,

 

I've read several posts related to this same topic and used the solutions posted.

No success yet.

 

How do I modify the date listed to use a date formatter so I can show something like "July 4" as opposed to "2013-07-04 03:00:00 +0000" ?

 

To be clear I'm wanting to change the display of the main list of objects. The detail view is behaving as required.

 

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ipad-BG-pattern.png"]];
    [self.tableView setBackgroundColor:bgColor];
    
    self.title = @"Records";
    self.navigationBarType = SCNavigationBarTypeAddEditRight;
    
    NSManagedObjectContext *context = [(id)[UIApplication sharedApplication].delegate managedObjectContext];
    SCEntityDefinition *dailyRecordDef = [SCEntityDefinition definitionWithEntityName:@"Day" managedObjectContext:context autoGeneratePropertyDefinitions:YES];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    //dateFormatter.dateFormat = @"MMM dd";

    dailyRecordDef.titlePropertyName = @"date";
    
    SCPropertyDefinition *datePropertyDef = [dailyRecordDef propertyDefinitionWithName:@"date"];
    datePropertyDef.attributes = [SCDateAttributes attributesWithDateFormatter:dateFormatter];
    
    SCArrayOfObjectsModel *objectsModel = [SCArrayOfObjectsModel modelWithTableView:self.tableView entityDefinition:dailyRecordDef];
    //objectsModel.searchPropertyName = @"name";
	objectsModel.addButtonItem = self.addButton;
    objectsModel.editButtonItem = self.editButton;
	objectsModel.autoSortSections = TRUE;
    //objectsModel.detailViewControllerOptions
    
    self.tableViewModel = objectsModel;
}

- (void)tableViewModel:(SCTableViewModel *)tableViewModel willConfigureCell:(SCTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([cell isKindOfClass:[SCDateCell class]])
    {
        SCDateCell *dateCell = (SCDateCell *)cell;
        [dateCell.dateFormatter setDateFormat:@"MMM dd, yyyy"];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[self.tableViewModel reloadBoundValues];
}

 

 



#2 aaronium112

aaronium112

    Jr. Member

  • STV 3.0 Std
  • PipPip
  • 21 posts
Reputation: 7
Good

Posted 06 April 2014 - 01:03 PM

Here's a working solution.

Using cellAction.willConfigure to style the cell as desired.


-(void)viewDidLoad
{
    self.title = @"Time Slicer";
    self.navigationBarType = SCNavigationBarTypeAddEditRight;
    NSManagedObjectContext *context = [(id)[UIApplication sharedApplication].delegate managedObjectContext];
    
    //Record definition=====================================================================================
    
    SCEntityDefinition *repDef = [SCEntityDefinition definitionWithEntityName:@"Rep"
                                                         managedObjectContext:context
                                                          propertyNamesString:@"date"];
    
    [repDef propertyDefinitionWithName:@"date"].type = SCPropertyTypeDate;
    
    // Day definition============================================================================
    
    SCEntityDefinition *dayDef = [SCEntityDefinition definitionWithEntityName:@"Day"
                                                            managedObjectContext:context
                                                             propertyNamesString:@"Properties:(date);Reps:(reps)"];

    
    SCPropertyDefinition *datePropertyDef = [dayDef propertyDefinitionWithName:@"date"];
    datePropertyDef.type = SCPropertyTypeDate;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    datePropertyDef.attributes = [SCDateAttributes attributesWithDateFormatter:dateFormatter];
    
    dayDef.descriptionPropertyName = @"reps";
    dayDef.keyPropertyName = @"date";
    dayDef.titlePropertyName = @"date";
    
    //[dayDef propertyDefinitionWithName:@"date"].type = SCPropertyTypeDate;
    
    SCPropertyDefinition *repsRelDef = [dayDef propertyDefinitionWithName:@"reps"];
    repsRelDef.title = @"Reps";
    repsRelDef.type = SCPropertyTypeArrayOfObjects;
    repsRelDef.attributes = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:repDef
                                                                         allowAddingItems:YES
                                                                       allowDeletingItems:YES
                                                                         allowMovingItems:YES];
    
    // Section===============================================================================
    
    SCArrayOfObjectsSection *section = [SCArrayOfObjectsSection sectionWithHeaderTitle:@"Day" entityDefinition:dayDef];
    
    section.addButtonItem = self.addButton;
    section.placeholderCell = [SCTableViewCell cellWithText:@"No Days Yet!" textAlignment: NSTextAlignmentCenter];
    section.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        // Add the cell's index before its title
        Day *dayEntity = (Day*)[cell boundObject];
        NSDate *day = dayEntity.date;
        NSString *title = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:day]];
        cell.textLabel.text = title;
        cell.detailTextLabel.text = @"9 pH     432 Hz";
    };
    // Add Sections to TableViewModel===============================================================================
    
    [self.tableViewModel addSection:section];

}






Also tagged with one or more of these keywords: core-data

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users