ios - 时间戳一直在计数

标签 ios tableview tablecell

我有一个内置了下载管理器的应用程序。下载文件后,它会保存到文档文件夹内的文件夹中。现在我已经在我的表格 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/

相关文章:

ios - 根据(城市)从 firebase 检索数据

JavaFX TableView 文本对齐

java - 如何根据 TableView 中的行元素自定义表格单元格/表格列内容(JavaFX)

java - 如何在 JavaFX 中将 CheckBox 添加到 TableView

ios - 激活搜索栏时会出现隐藏状态栏

ios - 如何将文件保存到 iOS 的 Library/NoCloud 文件夹?

ios - 在 Swift 中选择时关闭 UITableView Cell?

ios - 从本地应用程序文档文件夹中删除文件

ios - 强制改变场景之间的界面方向

c# - 在 C# 中迭代​​ RadGrid 中的每个单元格