我正在编写的iOS应用程序将日期保存到Core Data,然后计算“今天之间的时间”和TableView中显示的“存储的日期”。
我能够成功检索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)
在自定义单元格中,我想timeBetweenDatesRealTime
通过NSTimer函数每1秒调用一次函数(该函数计算存储日期和今天之间的时间并将结果显示在标签中)(有关的设置方法,请参见此处)但我无法访问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)
}
}
如果您使用情节提要板来配置单元格,则需要将表视图单元格的类更改为情节提要中的自定义类。
如果您不使用情节提要,则应使用以下方法注册自定义单元格类: registerClass:forCellReuseIdentifier:
完成此操作后,表视图应返回您的自定义类,以便您可以像下面这样修改代码: var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell
然后,您可以countdownEntry
在CountdownTableViewCell
类中创建属性,以便可以在中设置该属性tableView:cellForRowAtIndex:
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句