在UITableView单元格中格式化日期和时间 [英] Formatting date and time in UITableView cells

查看:144
本文介绍了在UITableView单元格中格式化日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 tableView:cellForRowAtIndexPath:中格式化日期和时间。因为创建一个 NSDateFormatter 是一个相当重的操作,我已经使它们是静态的。这是每行格式化日期和时间的最佳方法吗?

I need to format both date and time in a tableView:cellForRowAtIndexPath:. Since creating an NSDateFormatter is a fairly heavy operation, I've made them static. Is this the best approach to formatting a date and time on a per-row basis?

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


推荐答案

静态变量,因为那样你几乎肯定会遇到内存泄漏。相反,我将使用两个 NSDateFormatter * 实例变量或在该控制器对象上的属性,仅在需要时实例化。当视图卸载或控制器被释放时,您可以释放它们。

I wouldn't use a static variable, because then you'll almost certainly end up with a memory leak. Instead, I would use two NSDateFormatter * instance variables or properties on that controller object that are instantiated only on demand. When the view unloads or the controller is deallocated, you can then release them.

例如:

@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

这篇关于在UITableView单元格中格式化日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆