2010-12-15 27 views
1

我需要在tableView:cellForRowAtIndexPath:中格式化日期和時間。由於創建NSDateFormatter是一項相當繁重的操作,因此我將它們設置爲靜態。這是以行爲單位格式化日期和時間的最佳方法嗎?在UITableView單元中格式化日期和時間

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

回答

7

我不會使用靜態變量,因爲那樣你幾乎肯定會發生內存泄漏。相反,我會在該控制器對象上使用兩個僅在需求時實例化的實例變量或屬性。當視圖卸載或控制器被釋放時,您可以釋放它們。

例如:

@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 
+1

爲什麼使用靜態變量會導致內存泄漏?您能否詳細說明您的建議解決方案?我並沒有真正遵循你的方法。 – memmons 2010-12-15 19:30:01

+0

@Harkonian使用靜態變量會導致內存泄漏,因爲一旦完成使用它們,就無法釋放日期格式化程序。即使您的表格視圖只能看到一次,它的日期格式化程序也會在應用程序的其餘生命週期中停留。我將編輯我的答案,以包含視圖控制器的一些示例代碼。 – 2010-12-15 20:02:39

+0

不確定我們想要擁有和使用的變量是否是內存泄漏本身。 – QED 2013-04-27 14:57:01

2

我已經在不同的地方看,如果 您使用NSDateFormatter很多, 你應該建立一個靜態變量, 但在測試這個方法,我發現它 使用了很多更記憶。

但是在你的代碼中你並沒有爲你的格式化程序使用靜態變量。請嘗試以下修改:

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層格式的實例中所有運行時間將手),但創建格式是繁重的操作本身所以這種方式顯著提高你的方法表現