设置UITableViewCell字幕值等于系统日期和时间

时间:2014-12-05 10:12:41

标签: ios objective-c uitableview

我在目标c中很新。我正在使用这个link的故事板项目。我在故事板上添加了字幕。如何在创建单元格时选择系统日期和时间并显示它作为字幕细胞的价值?

3 个答案:

答案 0 :(得分:0)

您可以致电

获取当前时间
[NSDate date];

然后,您可以使用 NSDateFormatter 将其格式化为字符串,例如

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *date = [formatter stringFromDate:[NSDate date]];

请注意,NSDateFormatter是一个昂贵的实例化类,因此您应该在使用之前进行准备。

答案 1 :(得分:0)

[NSDate date]将在创建单元格时返回系统日期(模拟器,设备)。见下面的代码:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDate *date = [NSDate date];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",date]];

    return cell;
}

答案 2 :(得分:0)

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]];

将打印系统日期和时间。如果要更改格式,请使用NSDateFormatter

相关问题