UA-17470720-3

Jump to content


Photo
- - - - -

Best place to format dates on SCDateCell

cells

  • Please log in to reply
2 replies to this topic

#1 sumowesley

sumowesley

    Sr. Member

  • STV 4.0 Std
  • PipPipPip
  • 33 posts
Reputation: 2
Good

Posted 07 April 2015 - 12:43 PM

I have a SCTableViewController with a number of date fields on it. I would really like to have them display the dates the same way. To try out my thoughts I came up with this:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateStyle:NSDateFormatterShortStyle];

    SCDataDefinition *taskEntityDef = [self dataDefinitionWithIBName:@"EntityObjectivePeriod"];
    taskEntityDef.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        if([cell isKindOfClass:[SCDateCell class]]) {
            SCDateCell *dc = (SCDateCell *)cell;
            dc.dateFormatter = df;
        }
        
    };
    
}

Is this a reasonable method or is there a more elegant way to set the format of these dates? Over time, I will use a setting to format the dates but short format is fine for now.



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 07 April 2015 - 01:33 PM

The date formatter code you wrote is really good and already shows that you're starting to have solid STV understanding already. It's very common to find that STV provides several ways to achieve the same result, so I'll list a couple more here too:

 

1. If you don't require any special runtime customization to the date formatter, then it might be easier to just set the date format string in IB. To do that, select the date property definition from IB and enter the date format in the "Date Formatter" field.

 

IBDateFormatter.png

 

2. In addition to assigning the action to the whole data definition, you could also assign it to the individual property definition. This way you won't need to check if the cell is of type SCDateCell. For example:

 

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];

SCDataDefinition *taskEntityDef = [self dataDefinitionWithIBName:@"EntityObjectivePeriod"];
SCPropertyDefinition *datePropertyDef = [taskEntityDef propertyDefinitionWithName:@"yourDateAttributeName"];
datePropertyDef.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
  SCDateCell *dc = (SCDateCell *)cell;
  dc.dateFormatter = df;
};


#3 sumowesley

sumowesley

    Sr. Member

  • STV 4.0 Std
  • PipPipPip
  • 33 posts
Reputation: 2
Good

Posted 07 April 2015 - 10:19 PM

That's useful to know, thank you.







Also tagged with one or more of these keywords: cells

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users