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静的変数を使用すると、日付フォーマッタを使用し終わった後に日付フォーマッタを解放する方法がないため、メモリリークが発生します。テーブルビューが1回しか見えなくても、残りのアプリケーションライフタイムの間、その日付フォーマッタはぶら下がります。ビューコントローラのサンプルコードを含めるように答えを編集します。 –
私たちが持っていて使いたい変数が参照されているかどうかは、メモリリークそのものです。 – QED