I need to format both date and time in a tableView:cellForRowAtIndexPath:
. Since creating an NSDateFormatter
is a fairly heavy operation, I've made them static. Is this the best approach to formatting a date and time on a per-row basis?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyCell*cell = (MyCell*)[self.tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
}
cell.dateLabel = [dateFormatter stringFromDate:note.timestamp];
static NSDateFormatter *timeFormatter = nil;
if (!timeFormatter)
{
timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
}
cell.timeLabel = [timeFormatter stringFromDate:note.timestamp];
return cell;
}
But in your code you don't use static variables for your formatters. Try the following modification:
This may not save you memory (as your 2 formatter instances will hand during all run-time period), but creating formatter is heavy operation itself so this approach significantly improves your method performance
You can use this to handle reuse of NSDateFormatter's: https://github.com/DougFischer/DFDateFormatterFactory#readme
P.S: Since you set only format and locale to your data formatters.
I wouldn't use a static variable, because then you'll almost certainly end up with a memory leak. Instead, I would use two
NSDateFormatter *
instance variables or properties on that controller object that are instantiated only on demand. When the view unloads or the controller is deallocated, you can then release them.For example: