【问题标题】:Storing / Retrieving Core Data in Custom Cell in Swift在 Swift 中的自定义单元格中存储/检索核心数据
【发布时间】:2015-02-22 02:18:04
【问题描述】:

我正在编写的 iOS 应用程序将日期保存到 Core Data,然后计算 Time Between Today 和 TableView 中显示的 Stored Date

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

当我从标准单元更改为自定义单元时会出现问题。我不确定如何将核心数据传输到每个自定义单元实例。或者,如果核心数据自动传输到每个自定义单元格,我不确定如何访问变量。

这就是我在更改为有效的自定义单元之前的方式:

// 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 函数 (see here for how I set this up, if relevant) 每 1 秒调用一次 timeBetweenDatesRealTime 函数(计算存储日期和今天之间的时间并在标签中显示结果),但是我无法访问countdownEntry.date

这是我的自定义单元格类:

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

}

【问题讨论】:

    标签: uitableview swift core-data custom-cell


    【解决方案1】:

    如果您使用情节提要配置单元格,则需要将表格视图单元格的类更改为情节提要中的自定义类。

    如果您不使用情节提要,您应该使用registerClass:forCellReuseIdentifier: 注册您的自定义单元类

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

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

    【讨论】:

    • 所以我正在使用情节提要并在 Identity Inspector 中使用自定义类“CountdownTableViewCell”。我修改了代码:var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell,如上所述,它可以工作。为了创建属性,我将 var countdownEntry:CountdownEntry = CountdownEntry() 添加到 CountdownTableViewCell。在我的表格视图中,我有 let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry 但我遇到了崩溃
    • error: Failed to call designated initializer on NSManagedObject class 'CountdownEntry'
    • [CountdownEntry date]: unrecognized selector sent to instance 0x7fe373e549e0
    • Barum 通过消息提供帮助,我想发布最终解决方案以防有人想知道。在生成需要的单元格的tableView中:var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry cell.countdownEntry = countdownEntry 而在自定义单元格中,需要添加var countdownEntry = CountdownEntry?()
    • 关键是这一行cell.countdownEntry = countdownEntry,它将信息传递给自定义单元格。您需要var countdownEntry = CountdownEntry?(),以便设置单元以接收信息。
    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 2019-07-11
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多