The iOS app I'm writing saves dates to Core Data then calculates the Time Between Today and the Stored Date displayed in a TableView.
I am able to successfully retrieve the dates in Core Data and display the TimeBetween for each StoredDate in the TableView.
The problem occurs when I changed from standard cells to Custom Cells. I'm not sure how to transfer the Core Data to each of the Custom Cell instances. Or, if the Core Data is automatically transferred to each Custom Cell, I'm not sure how to access the variables.
This is how I had it before changing to Custom Cells which was working:
// Generate the cells
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as UITableViewCell
let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry
// Grab the elements using the tag
let labelCountdown:UILabel? = cell.viewWithTag(1) as UILabel?
let labelName:UILabel? = cell.viewWithTag(2) as UILabel?
let iconImage:UIImageView? = cell.viewWithTag(3) as UIImageView?
labelCountdown.text = CountdownEngine.timeBetweenDatesRealTime(countdownEntry.date)
In the Custom Cell, I want to call the timeBetweenDatesRealTime
function (which calculates the time between the Stored Date and Today and displays the result in a label) every 1 second via a NSTimer function (see here for how I set this up, if relevant), but I can't access countdownEntry.date
.
Here is my custom cell class:
import UIKit
class CountdownTableViewCell: UITableViewCell {
// Outlets
@IBOutlet weak var iconImage: UIImageView!
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var labelCountdown: UILabel!
// Counter Variable
var timeInterval: NSTimeInterval = 0 {
didSet {
labelCountdown.text = "\(timeInterval)"
}
}
func updateUI() {
println("updating custom cell")
/*
// Show real-time countdown of time remaining between today and saved date
labelCountdown.text = CountdownEngine.timeBetweenDatesRealTime(countdownEntry.date)
}
*/
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
println("code from cell called")
// add listener
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: Selector("updateUI"), name: "CustomCellUpdate", object: nil)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// MARK: self-cleanup
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}