날짜와 시간을 모두 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;
}
정적 막대를 사용하면 메모리 누수가 발생하는 이유는 무엇입니까? 제안 된 솔루션에 대해 자세히 설명해 주시겠습니까? 나는 당신의 접근 방식을 따르고 있지 않습니다. – memmons
@Harkonian 정적 변수를 사용하면 메모리 누수가 발생할 수 있습니다. 왜냐하면 일단 날짜 포맷터를 사용한 후에는 날짜 포맷을 해제 할 방법이 없기 때문입니다. 테이블보기가 한 번만 표시되는 경우에도 날짜 서식자가 나머지 응용 프로그램 수명 동안 계속 지연됩니다. 보기 컨트롤러에 대한 몇 가지 샘플 코드를 포함하도록 답변을 수정합니다. –
우리가 가지고 있고 사용하기를 원하는 참조 변수가 그 자체가 메모리 누수인지 확실치 않습니다. – QED