在 Swift 中的自定义单元格中存储/检索核心数据 [英] Storing / Retrieving Core Data in Custom Cell in Swift

查看:35
本文介绍了在 Swift 中的自定义单元格中存储/检索核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写的 iOS 应用将日期保存到 Core Data,然后计算 时间间隔 今天和 存储日期 显示在 TableView 中.

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.

我能够成功检索 Core Data 中的日期并在 TableView 中显示每个 StoredDate 的 TimeBetween.

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)

在自定义单元格中,我想通过 NSTimer 函数每 1 秒调用一次 timeBetweenDatesRealTime 函数(它计算存储日期和今天之间的时间并将结果显示在标签中)a href="https://stackoverflow.com/questions/28534518/how-to-update-uitableviewcells-using-nstimer-and-nsnotificationcentre-in-swift" title="Link">请参阅此处了解我如何设置,如果相关),但我无法访问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)
}

}

推荐答案

如果你使用 storyboard 来配置单元格,你需要将 table view cell 的类更改为 storyboard 中的自定义类.

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.

如果您不使用故事板,您应该使用 registerClass:forCellReuseIdentifier:

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

完成此操作后,表视图应返回您的自定义类,以便您可以像下面这样修改代码:<代码>var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell

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

然后,您可以在CountdownTableViewCell 类中为countdownEntry 创建属性,以便您可以在tableView:cellForRowAtIndex: 中设置该属性.

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

这篇关于在 Swift 中的自定义单元格中存储/检索核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆