Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

iphone - Formatting date and time in UITableView cells

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

@interface MyViewController : UITableViewController {
    NSDateFormatter *dateFormatter;
    NSDateFormatter *timeFormatter;
}

@end

@implementation MyViewController
- (void)viewDidUnload {
    // release date and time formatters, since the view is no longer in memory
    [dateFormatter release]; dateFormatter = nil;
    [timeFormatter release]; timeFormatter = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    // release date and time formatters, since this view controller is being
    // destroyed
    [dateFormatter release]; dateFormatter = nil;
    [timeFormatter release]; timeFormatter = nil;
    [super dealloc];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...

    // if a date formatter doesn't exist yet, create it
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[NSLocale currentLocale]];
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    }

    cell.dateLabel = [dateFormatter stringFromDate:note.timestamp];

    // if a time formatter doesn't exist yet, create it
    if (!timeFormatter) {
        timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
    }

    cell.timeLabel = [timeFormatter stringFromDate:note.timestamp];
    return cell;
}

@end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...