[Swift] Countdown????

di il
1 risposte

[Swift] Countdown????

Come si crea un countdown da data corrente a una data di scadenza?
Tra le altre cose vorrei implementarla in una tableView

1 Risposte

  • Re: [Swift] Countdown????

    Ho trovato una soluzione ma c'è ancora qualcosa che non va... il timer non parte, perché?
    import UIKit
    import Foundation
    
    class Testios: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var Tabella: UITableView!
        
        var lista: [String] = []
        var a: String?
        var b: String?
        let currentDate = Date()
        let dateFormatter = DateFormatter()
        
        let userCalendar = Calendar.current
        
        let requestedComponent: Set<Calendar.Component> = [.day,.hour,.minute,.second,.month,.year]
        
    
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return lista.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Testios", for: indexPath)
            cell.textLabel?.text = lista[indexPath.row]
            return cell
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true)
            timer.fire()
        }
        
        func printTime()
        {
            dateFormatter.dateFormat = "yyyy/MM/dd hh:mm:ss"
            let startTime = Date()
            let endTime = dateFormatter.date(from: "2018/02/01 00:00:00")
            let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)
            
            let dateLabelOutlet = "\(timeDifference.year!) Years \(timeDifference.month!) Months \(timeDifference.day!) Days \(timeDifference.hour!) Hours \(timeDifference.day!) Days \(timeDifference.hour!) Hours \(timeDifference.minute!) Minutes \(timeDifference.second!) Seconds"
            lista.append(dateLabelOutlet)
    }
     
    }
    
Devi accedere o registrarti per scrivere nel forum
1 risposte