2010-12-15 5 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

저는 정적 변수를 사용하지 않을 것입니다. 왜냐하면 메모리 누출로 거의 확실하게 끝날 것이기 때문입니다. 대신에, 필요할 때만 인스턴스화되는 컨트롤러 객체에 두 개의 NSDateFormatter * 인스턴스 변수 또는 속성을 사용합니다. 뷰가 언로드되거나 컨트롤러의 할당이 해제되면 해제 할 수 있습니다. 예를 들어

: https://github.com/DougFischer/DFDateFormatterFactory#readme

P.S :

@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

+0

@Harkonian 정적 변수를 사용하면 메모리 누수가 발생할 수 있습니다. 왜냐하면 일단 날짜 포맷터를 사용한 후에는 날짜 포맷을 해제 할 방법이 없기 때문입니다. 테이블보기가 한 번만 표시되는 경우에도 날짜 서식자가 나머지 응용 프로그램 수명 동안 계속 지연됩니다. 보기 컨트롤러에 대한 몇 가지 샘플 코드를 포함하도록 답변을 수정합니다. –

+0

우리가 가지고 있고 사용하기를 원하는 참조 변수가 그 자체가 메모리 누수인지 확실치 않습니다. – QED

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 개 포맷터 인스턴스로) 당신이 메모리를 저장하지 수 있지만, 생성 포맷은 무거운 작업 자체가

다음 수정을 시도
0

당신은 NSDateFormatter의 재사용을 처리하기 위해 이것을 사용할 수 있습니다 당신은 당신의 데이터 포매터 만 형식과 로케일을 설정입니다.