上周在 Parse 中创建内容时在单元格中显示图像 - Swift

Display an image in a cell when the content was created in Parse within the last week - In Swift

让我解释一下,我正在使用 Parse,它为我保存了一些电影评论。我将这些填充到 Parse 的 UITableView 控制器中。一切正常 - 没问题。

我想做什么...

仅当该对象是上周在 Parse 中创建时,才在单元格 (本地保存) 中显示另一个图像。如此重要,我想添加的附加图像是一个小横幅,上面写着 'New'。如果我在当月 1 日将影片添加到我的 Parse 数据库中,那么我希望此 'New' 图像横幅在接下来的 7 天内也显示在单元格中。在第 8 天,'New' 图像横幅被删除,它只是正常显示单元格。

我知道在 Parse 中有一个自动列,它为每个名为 "createdAt" 的对象创建,所以我认为这是我需要处理的部分。我只是不知道如何将其添加为代码,因为我从未真正使用过 Parse 中的日期。

单元格的显示方式如下,供参考:

所以我只希望 'New' 红色图像从 Parse 的 "createdAt" 日期起显示 7 天。

谁能帮我看看如何添加这段代码?

提前致谢。

更新:

编辑:我的 CustomTableViewCell.swift

class CustomTableViewCell: PFTableViewCell {

@IBOutlet weak var filmNameLabel: UILabel!

@IBOutlet weak var filmReleaseDateLabel: UILabel!

@IBOutlet weak var directedByLabel: UILabel!

@IBOutlet weak var cellFilmImage: PFImageView!


// MARK: - Star Rating Images

@IBOutlet weak var star1: UIImageView!

@IBOutlet weak var star2: UIImageView!

@IBOutlet weak var star3: UIImageView!

@IBOutlet weak var star4: UIImageView!

@IBOutlet weak var star5: UIImageView!


@IBOutlet weak var bannerView: UIImageView!



func bindData(filmObject: PFObject) {

    if let filmName = filmObject["FilmName"] as? String {
        filmNameLabel.text = filmName
    } else {
        filmNameLabel.text = "Untitled"
    }

    if let dateReleased = filmObject["DateReleased"] as? String {
        filmReleaseDateLabel.text = "In UK cinemas \(dateReleased)"
    } else {
        filmReleaseDateLabel.text = "N/A"
    }

    if let directedBy = filmObject["DirectedBy"] as? String {
        directedByLabel.text = "Directed by \(directedBy)"
    } else {
        directedByLabel.text = "N/A"
    }

    // Show the banner if the object was created within the last 7 days
    if let createdAt = filmObject["createdAt"] as? NSDate {
        let calendar = NSCalendar.currentCalendar()
        let sevenDaysAgo = calendar.dateByAddingUnit(.Day, value: -7, toDate: NSDate(), options: [])

        // Show banner if sevenDaysAgo <= createdAt
        if sevenDaysAgo!.compare(createdAt) != .OrderedDescending {
            // Show the banner imageView
            bannerView.hidden = false
        } else {
            bannerView.hidden = true
        }
    } else {
        bannerView.hidden = true
    }



    // MARK: - Star Rating System
    if let ourRating = filmObject["OurRating"] as? Double {

        star1.image = getStarImage(1, forRating: ourRating)
        star2.image = getStarImage(2, forRating: ourRating)
        star3.image = getStarImage(3, forRating: ourRating)
        star4.image = getStarImage(4, forRating: ourRating)
        star5.image = getStarImage(5, forRating: ourRating)

    }

    let imageFromParse = filmObject.objectForKey("filmPosterImage") as? PFFile
    imageFromParse!.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
        let image: UIImage! = UIImage(data: imageData!)!
        self.cellFilmImage?.image = image
    })

}

}

我的 TableViewController.swift 显示 cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableViewCell!
    if cell == nil {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    if object != nil {
        cell.bindData(object!)
    }

    return cell
}

应该像将 UIImageView 添加到您的单元格(通过故事板原型、xib 或任何您喜欢的方式)一样直接,并使用从您项目的图像资产加载的横幅。

初始化要隐藏的横幅 imageView,然后在您的 tableView 中 cellForRowAtIndexPath 检查关联的 review 是否是在过去 7 天内创建的。根据您是使用标准 UITableView 并自己查询评论,还是使用来自 ParseUI 框架的 PFQueryTableViewController,具体实现会有所不同,但其要点如下。

let calendar = NSCalendar.currentCalendar()
let sevenDaysAgo = calendar.dateByAddingUnit(.Day, value: -7, toDate: NSDate(), options: [])

if let reviewDate = object?.createdAt {
    // sevenDaysAgo <= reviewDate
    if sevenDaysAgo.compare(reviewDate) != .OrderedDescending {
        // Show the banner imageView
        cell.bannerView.hidden = false
    }
}

编辑:这是 cellForRowAtIndexPathCustomTableViewCell

的更新版本
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableViewCell!
    if cell == nil {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    if object != nil {
        cell.bindData(object!)
    }
    return cell
}

class CustomTableViewCell: PFTableViewCell {

    @IBOutlet weak var filmNameLabel: UILabel!
    @IBOutlet weak var filmReleaseDateLabel: UILabel!
    @IBOutlet weak var directedByLabel: UILabel!
    @IBOutlet weak var bannerView: UIImageView!
    // ... other outlets

    func bindData(filmObject: PFObject) {

        if let filmName = filmObject["FilmName"] as? String {
            filmNameLabel.text = filmName  
        } else {
            filmNameLabel.text = "Untitled"
        }

        if let dateReleased = filmObject["DateReleased"] as? String {
            filmReleaseDateLabel.text = "In UK cinemas \(dateReleased)"  
        } else {
            filmReleaseDateLabel.text = "N/A"
        }

        if let directedBy = filmObject["DirectedBy"] as? String {
            directedByLabel.text = "Directed by \(directedBy)"  
        } else {
            directedByLabel.text = "N/A"
        }

        // Show the banner if the object was created within the last 7 days
        if let createdAt = filmObject.createdAt {
            let calendar = NSCalendar.currentCalendar()
            let sevenDaysAgo = calendar.dateByAddingUnit(.Day, value: -7, toDate: NSDate(), options: [])

            // Show banner if sevenDaysAgo <= createdAt
            if sevenDaysAgo!.compare(createdAt) != .OrderedDescending {
                // Show the banner imageView
                bannerView.hidden = false
            } else {
                bannerView.hidden = true
            }
        } else {
            bannerView.hidden = true
        }

        // ... bind the rest of the data from the PFObject to the cell

    }

}