So let me explain, I am using Parse, which holds some film reviews for me. I am populating these into a UITableView controller from Parse. This all works fine - no issues.
What I'd like to do...
Display another image in the cell (held locally) only when that object was created in Parse within the last week. So essential, the additional image I want to add is a small banner saying 'New'. If I added the film to my Parse database on the 1st of the month, then I want this 'New' image banner to also be displayed for the next 7 days in the cell. On the 8th day, the 'New' image banner removes and it just displays the cell as normal.
I know there is a automatic column in Parse that gets created for every object called "createdAt"
, so I assume this is the part I need to work with. I just can't figure out how to add this as code, as I've never really worked with Dates from Parse.
Below is how the cell would be displayed for reference:
So I just want that 'New' image in red displayed for 7 days from the "createdAt"
date from Parse.
Can anyone help show me how to add this code?
Thanks in advance.
UPDATED:
EDIT: My 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
})
}
}
My TableViewController.swift
showing 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
}