在UITableView单元格中格式化date和时间

我需要在tableView:cellForRowAtIndexPath:格式化date和时间。 由于创build一个NSDateFormatter是一个相当繁重的操作,我已经使他们静态。 这是以行为单位格式化date和时间的最佳方法吗?

 - (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; } 

我不会使用一个静态variables,因为那你几乎肯定会发生内存泄漏。 相反,我会在该控制器对象上使用两个NSDateFormatter *实例variables或属性,这些variables或属性仅在需求时实例化。 当视图卸载或控制器被释放时,您可以释放它们。

例如:

 @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 

我已经在各种地方读过,如果你使用NSDateFormatter很多,你应该设置一个静态variables,但是在testing这个方法时,我发现它耗尽了更多的内存。

但是在你的代码中你不使用静态variables来处理你的格式化程序。 尝试以下修改:

 static NSDateFormatter *dateFormatter = nil; if (!dateFormatter){ dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[NSLocale currentLocale]]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; } cell.dateLabel = [dateFormatter stringFromDate:note.timestamp]; // And same approach for timeFormatter 

这可能不会节省您的内存(因为您的2个格式化程序实例将在所有运行时间段内处理),但是创build格式化程序本身是繁重的操作,所以这种方法显着提高了您的方法性能

你可以用它来处理NSDateFormatter的重用: https : //github.com/DougFischer/DFDateFormatterFactory#readme

PS:由于您只设置格式和区域设置到您的数据格式化程序。