在 UITableView 单元格中设置日期和时间的格式
我需要在 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会使用静态变量,因为那样你几乎肯定会导致内存泄漏。相反,我会在该控制器对象上使用两个仅根据需要实例化的
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.For example:
但在您的代码中,您不为格式化程序使用静态变量。尝试以下修改:
这可能不会节省您的内存(因为您的 2 个格式化程序实例将在所有运行时期间进行处理),但创建格式化程序本身就是繁重的操作,因此这种方法显着提高了您的方法性能
But in your code you don't use static variables for your formatters. Try the following modification:
This may not save you memory (as your 2 formatter instances will hand during all run-time period), but creating formatter is heavy operation itself so this approach significantly improves your method performance
您可以使用它来处理 NSDateFormatter 的重用:
https://github.com/DougFischer/DFDateFormatterFactory#readme
PS:因为您只设置了格式和您的数据格式化程序的区域设置。
You can use this to handle reuse of NSDateFormatter's:
https://github.com/DougFischer/DFDateFormatterFactory#readme
P.S: Since you set only format and locale to your data formatters.