如何在UITableView单元格中显示当前时区

问题描述

我正在尝试创建一个时区应用程序,该应用程序将以秒为单位显示当前时间,我能够在标签上显示秒,但所有行都显示相同的时间。我尝试将时区和当前时间添加到数组中,但是我想不出一种方法来每秒更新数组以显示当前时间

您可以在图片中看到,我需要几秒钟,几分钟和几小时来进行更新。

这是我到目前为止所得到的。

谢谢

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,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,selector: #selector(updateTimer),repeats: true)
        }
        
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)