Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...