Storing / Retrieving Core Data in Custom Cell in Swift

早过忘川 提交于 2019-12-11 09:34:32

问题


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

}

回答1:


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:.



来源:https://stackoverflow.com/questions/28653764/storing-retrieving-core-data-in-custom-cell-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!