2010-11-27 4 views
0

NSDateFormatter (또는 NSNumberFormatter) 할당은 비교적 느리고
모든 셀에 대해 cellForRowAtIndexPath가 실행됩니다.
따라서 cellForRowAtIndexPath에 포맷터를 할당하는 것은 불규칙 스크롤의 중요한 원인이 될 수 있습니다. 어떻게 cellForRowAtIndexPath가 포맷터를 재사용 할 수 있습니까 (스크롤 기능 향상)?

는 스크롤을 원활 I는 그들 클래스 변수를 만들고 viewWillAppear 그들을 할당하고 (아래 코드 참조) viewWillDisappear 그들을 해제함으로써, 그들
를 외부 cellForRowAtIndexPath 할당을 시도했다.
그러나 포맷터에서 누출이 발생합니다.

cellForRowAtIndexPath에 사용 된 포맷터를 선언/할당/해제하는 가장 좋은 장소는 어디입니까?

//in myNavigationViewController.m: 
NSDateFormatter *myDateFormatter;        

-...viewWillAppear...{      
    if(myDateFormatter){     //Solution: add this check. 
     [myDateFormatter release]; 
    } 
    myDateFormatter = [[NSDateFormatter alloc] init];       
    [myDateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [myDateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    if(locale){        //Solution: add this check. 
     [locale release]; 
    } 
    locale = [NSLocale currentLocale]; 
    [myDateFormatter setLocale:locale];  
} 
-...cellForRowAtIndexPath... { 
    cell.myDateLabel.text = [myDateFormatter stringFromDate:_date];    
} 

-...viewWillDisappear...{ 
// [myDateFormatter release];    //Solution: remove this line 
} 
-...dealloc { 
    [myDateFormatter release];    //Solution: add these 2 lines. 
    [locale release]; 
} 

답변

1

너무 당신이 -dealloc에 할당을 해제 기억하는 모든 누설해서는 안된다.

+0

감사합니다. a) viewWillDisappear에서 릴리즈를 제거하고, b) -dealloc에 ​​추가하고, c) viewWillAppear에서 myDateFormatter를 확인하고, d) 'locale'에 대해 동일한 작업을 수행하여 수정. 위의 코드에서 '솔루션'을 게시했습니다. 많은 감사합니다! – sambaMan

관련 문제