【问题标题】:Displayig NSDate from CoreData in TableView Subtitle在 TableView 字幕中显示来自 Core Data 的 NSDate
【发布时间】:2014-03-14 21:57:51
【问题描述】:

我想用这段代码在我的 TableView 中显示 CoreData:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = [self.inventory objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"InventoryProductName"]]];
    [cell.detailTextLabel setText:[device valueForKey:@"expireDate"]];

    return cell;
}

应用程序当然会崩溃,因为 expireDate 是一个日期而不是字符串/文本。 如何在 TableView 单元格的副标题中显示我的内容的过期日期?

【问题讨论】:

  • 使用 NSDateFormatter

标签: uitableview core-data nsdate


【解决方案1】:

这样设置日期:

[cell.detailTextLabel setText:[NSDateFormatter localizedStringFromDate:[device valueForKey:@"expireDate"] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]];

更多信息here

【讨论】:

    【解决方案2】:

    NSDateFormatter * 格式=[[NSDateFormatter alloc] init]; [格式 setDateFormat:@"MMM dd ,yyyy HH:mm"]; NSDate * now =[device valueForKey:@"expireDate"]; NSString *dateString=[格式 stringFormDate;now];

    【讨论】:

    • 我在哪里插入我保存的日期?
    • NSDate * now =[device valueForKey:@"expireDate"];
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多