1

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)
}

}
Community
  • 1
  • 1
learning_swift
  • 107
  • 1
  • 5

1 Answers1

0

If you are using a storyboard to configure the cells, you need to change the class of table view cell to your custom class in storyboard.

If you are not using a storyboard, you should register your custom cell class using registerClass:forCellReuseIdentifier:

Once you've done that, the table view should return your custom class so that you can modify your code like the following: var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell

And then, you can create property for countdownEntry in CountdownTableViewCell class, so that you can set that property in tableView:cellForRowAtIndex:.

Barum Rho
  • 2,743
  • 1
  • 20
  • 21
  • So I am using storyboard and am using the custom class "CountdownTableViewCell" in the Identity Inspector. I modified the code: `var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell` as described, and it works to that point. To create the property I added `var countdownEntry:CountdownEntry = CountdownEntry()` to CountdownTableViewCell. And in my table view I have `let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry` but I get a crash – learning_swift Feb 23 '15 at 02:37
  • `error: Failed to call designated initializer on NSManagedObject class 'CountdownEntry'` – learning_swift Feb 23 '15 at 02:38
  • `[CountdownEntry date]: unrecognized selector sent to instance 0x7fe373e549e0` – learning_swift Feb 23 '15 at 02:40
  • Barum helped through messages and I wanted to post the final solution in case anyone wanted to know. In the tableView where you generate the cells you need: `var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry cell.countdownEntry = countdownEntry` And in the Custom Cell, you need to add `var countdownEntry = CountdownEntry?()` – learning_swift Feb 23 '15 at 03:40
  • The key is this line `cell.countdownEntry = countdownEntry` which passes the info to the custom cell. And you need `var countdownEntry = CountdownEntry?()` so the cell is set up to receive the info. – learning_swift Feb 23 '15 at 03:42