iphone - 在 UITableView 单元格中格式化日期和时间

标签 iphone ios objective-c cocoa-touch nsdateformatter

我需要在 tableView:cellForRowAtIndexPath: 中设置日期和时间的格式。由于创建 NSDateFormatter 是一项相当繁重的操作,因此我将它们设为静态。这是按行格式化日期和时间的最佳方法吗?

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

最佳答案

我不会使用静态变量,因为那样你几乎肯定会以内存泄漏告终。相反,我会在该 Controller 对象上使用两个仅按需实例化的 NSDateFormatter * 实例变量或属性。当 View 卸载或 Controller 被释放时,您可以释放它们。

例如:

@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

关于iphone - 在 UITableView 单元格中格式化日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453550/

相关文章:

iphone - 修改 UIBezierPath 的图案透视图

iphone - UITableViewCell ClipsToBound 不起作用

ios - 什么时候从 2 个不同的类中获取 obtainPermanentIDsForObjects :error: ever needed??

ios - 推送通知不适用于生产

ios - 在 MKMapSnapshotter 上绘制一个 MKCircleRenderer

objective-c - iOS 在纵向时返回横向

javascript - Android 或 ios 平台上的 Node -WebKit 应用程序

iphone - 如何检测 View 中任意位置的点击?

ios - 使用 EXC_BAD_ACCESS 的核心数据 deleteObject 崩溃应用程序

ios - 有没有什么方法可以让服务器在没有应用程序请求数据的情况下与 iPhone 应用程序通信?