【发布时间】:2016-01-28 19:43:11
【问题描述】:
我正在获取以秒为单位存储在 Int 数组中的时间值,并尝试将其放在 UILabel 上,然后由 countDown 函数触发倒计时。我无法获得在每个单元格中倒计时的时间,也无法打印数组而不获取原始秒值而不格式化它,如下所示。现在我只能在每个单元格的 UILabel 上显示原始值:6532(小时、分钟、秒)
尝试在for loop 中单独打印数组时,我也无法格式化数组,如下所示。数组存储为 Int。
在 countDown 函数中使用 for 循环时,我可以将时间格式打印到日志中:HH:MM:SS。但并非如此。
这是我的倒计时功能:
func countDown() {
//timerInt is an array where I'm storing the Int values.
for i in 0 ..< timerInt.count {
let hours = timerInt[i] / 3600
let minsec = timerInt[i] % 3600
let minutes = minsec / 60
let seconds = minsec % 60
print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
timerInt[i]--
print(self.timerInt[i])
}
}
我正在尝试在 cellForRowAtIndexPath 方法中执行此操作。 UILabel 位于一个单独的类中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
//This is the first item i'm posting on a UILabel and it works fine.
myCell.productName.text = productName[indexPath.row]
/*This cell/label I'm unable to format like in the for loop
which is formatted as a string*/
myCell.secondLabel.text = ("\(intArray[indePath.row])")
/*how do I format the intArray here so that the time
appears HH:MM:SS?? */
return myCell
}
我如何格式化intArray,以便它以格式化时间 (HH:MM:SS) 的形式打印到日志和 UI,就像我使用 打印到日志中的 for loop 一样字符串格式化程序?
非常感谢任何帮助!
【问题讨论】:
标签: arrays string swift nsdate nsdateformatter