使用 NSDateFormatter 显示日期

Displaying The Date with NSDateFormatter

我想要做的是显示日期和时间,这与您在 Facebook 中看到的有些相似。但是,Xcode 显示此错误:

use of unresolved identifier 'creator'.

这是我遇到问题的函数:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell
    cell.post = posts[indexPath.row]
    cell.index = indexPath.row

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(creator.createdAt)

    return cell
}

直到我输入倒数第二行代码后才显示错误。具体来说,我知道错误是由 creator 引起的,但我也认为 timestampLabel 也是罪魁祸首,因为它没有像我在视频中跟随的那个人那样改变颜色。现在我正在利用 Parse 来存储所有用户数据,creator 是我在 Parse 中拥有的类别之一。我希望有人能为我指明正确的方向,因为我花了很多时间在可能非常简单的事情上。

罪魁祸首不是时间戳标签。罪魁祸首是创建者对象。

use of unresolved identifier 'creator'

表示该对象未在当前范围内定义。在您的情况下,创建者对象。应该定义创作者。

假设来自 youtube 视频和您的代码 posts 对象是一个解析对象数组 (PFObject)

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell
    cell.post = posts[indexPath.row]
    cell.index = indexPath.row
    //---------------------------Add this-------------------
    var creator = cell.post as PFObject
    //------------------------------------------------------

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(creator.createdAt)


    return cell
}