简体   繁体   English

如何为变量分配实时时间

[英]How to assign live time to a variable

I have a dictionary of values (dataValues) that I ultimately display in a table view. 我有一个最终在表视图中显示的值字典(dataValues)。 I need to display the current time as a string in that dictionary. 我需要在该字典中将当前时间显示为字符串。 dataValues gets its values from the Calculations app in a model file, where I perform my calculations. dataValues从模型文件中的Calculations应用程序获取其值,在其中执行我的计算。

I've tried appending currentTime to dataValues, but given it's a dictionary of type string:any it obvbiously doesn't work. 我尝试将currentTime追加到dataValues,但是鉴于它是string类型的字典:任何明显的方法都不起作用。

var dataValues : [String:Any]?

override func viewDidLoad() {
    super.viewDidLoad()

    dataValues = Calculations.shared.ecefToData()

    var timer = Timer()
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.tick) , userInfo: nil, repeats: true)
}
var currentTime: String = "00:00:00"
@objc func tick() -> String {
     currentTime = DateFormatter.localizedString(from: Date(),
                                                     dateStyle: .short,
                                                     timeStyle: .medium)
    return currentTime
    print(currentTime)

}

} }

I would like to append the current time to dataValues and have the time constantly update while in the app. 我想将当前时间附加到dataValues并在应用程序中不断更新时间。

You need to reload your tableView every time you update your currentTime value, but I'm not sure that's the best way to handle that. 每次更新currentTime值时,都需要重新加载tableView,但是我不确定这是处理该问题的最佳方法。

You could instead tie a timer directly to a label on your tableview cell and have it update repeatedly? 您可以改为将计时器直接绑定到表单元格上的标签上,并使其重复更新?

I solved my issue. 我解决了我的问题。 I simply chose to assign the current time to the detailView.text label if the index row was equal to zero as I wanted to display time in the top row of the table view. 如果索引行等于零,我只是选择将当前时间分配给detailView.text标签,因为我想在表格视图的第一行中显示时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM