我有一个内置了下载管理器的应用程序。下载文件后,它会保存到文档文件夹内的文件夹中。现在我已经在我的表格 View 单元格中实现了时间和日期戳。但是,当从一个 View Controller 切换回我的 TableView 时,时间和日期会更新为当前时间。
下面是我正在使用的代码,一如既往,我们将不胜感激。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } //did the subtitle style
NSUInteger row = [indexPath row];
cell.textLabel.text = [directoryContents objectAtIndex:row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM/d/yyyy, hh:mm aaa"];
NSDate *date = nil;
if (indexPath.row == 0)
{
date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.detailTextLabel.text = dateString;
}
return cell;
}
最佳答案
tableView:cellForRowAtIndexPath:
将在每次重新加载表格时调用,因此时间将更新为当前时间,因为 [NSDate date]
将被调用再次。
与其在 tableView:cellForRowAtIndexPath:
中计算日期,不如将其存储在其他地方(例如在类级别 NSMutableArray
中)并在文件下载完成时设置它。这样它就不会在每次加载表格时都被重置。
关于ios - 时间戳一直在计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17607270/