【发布时间】:2020-08-19 21:24:06
【问题描述】:
我正在尝试创建一个时区应用程序,该应用程序将显示当前时间,每秒钟计数一次,我能够在标签上显示秒数,但所有行显示相同的时间。我尝试将带有当前时间的时区附加到一个数组中,但我想不出一种方法来每秒更新一次数组以显示当前时间。
正如您在图片中看到的,我需要相应地更新 seconds、minutes 和 hours。
这是我目前得到的。
谢谢
import UIKit
class TimeZoneViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var timer: Timer?
@IBOutlet weak var timeZoneTableView: UITableView!
var zoneNameArray: [String] = ["Alpha Zone","Bravo Zone"]
// var offSetArray:[Int] = [1,2,3,4,5,6,7,8,9,10]
var timeArray:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
timeZoneTableView.delegate = self
timeZoneTableView.dataSource = self
// SupportedLocation.rowHeight = 69
}
override func viewWillAppear(_ animated: Bool) {
createTimer()
// createTimerTest()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return zoneNameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = timeZoneTableView.dequeueReusableCell(withIdentifier: "TimeZoneSegue",for: indexPath) as? TimeZoneTableViewCell {
//timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector:#selector(self.tick) , userInfo: nil, repeats: true)
print(timeArray.count)
cell.timeZoneNameLbl.text = zoneNameArray[indexPath.row]
//updateTimer()
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector:#selector(self.tick) , userInfo: nil, repeats: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 125
}
@objc func updateTimer() {
// 1
guard let visibleRowsIndexPaths = timeZoneTableView.indexPathsForVisibleRows else {
return
}
for indexPath in visibleRowsIndexPaths {
// 2
if let cell = timeZoneTableView.cellForRow(at: indexPath) as? TimeZoneTableViewCell {
// cell.updateTime()
// // Time
// //Local Time
let dateDate = Date()
let formatter = DateFormatter()
//
formatter.dateFormat = "HH:mm:ss"
let timeResults = formatter.string(from: dateDate)
// print("\(timeResults)")
//Zulu Time
let UTCDate = Date()
let formatterUTC = DateFormatter()
for offset in zoneNameArray{
switch offset {
case "Alpha Zone":
// print("Alpha")
formatterUTC.timeZone = TimeZone(abbreviation: "UTC+0")!
formatterUTC.locale = Locale(identifier: "en_US_POSIX")
formatterUTC.dateFormat = "HH:mm:ss"
let defaultTimeZoneStr = formatterUTC.string(from: UTCDate)
// cell.zoneTimeLbl.text = defaultTimeZoneStr
timeArray.append(defaultTimeZoneStr)
case "Bravo Zone":
print("Bravo")
formatterUTC.timeZone = TimeZone(abbreviation: "UTC+2")!
formatterUTC.locale = Locale(identifier: "en_US_POSIX")
formatterUTC.dateFormat = "HH:mm:ss"
let defaultTimeZoneStrBravo = formatterUTC.string(from: UTCDate)
// print("\(")l
// cell.zoneTimeLbl.text = defaultTimeZoneStrBravo
timeArray.append(defaultTimeZoneStrBravo) // defaultTimeZoneStrBravo
default:
break
}
cell.zoneTimeLbl.text = timeArray[indexPath.row]
}
}
}
}
func createTimer() {
// 1
if timer == nil {
// 2
timer = Timer.scheduledTimer(timeInterval: 1.0,
target: self,
selector: #selector(updateTimer),
userInfo: nil,
repeats: true)
}
}
}
【问题讨论】:
-
将计时器添加到 UILabel 本身
-
我没有工作,我可以使用该问题的答案来更新标签,但它只显示一个时区。如何将其应用于我所有时区所在的 TimeArray ? @LeoDabus
-
您可能使用的是同一个标签实例
-
@Roger ,我想我发现了这个错误。请检查我的答案。
标签: swift xcode uitableview timer