Update Label that present Time using Timer in a TableViewCell

I am trying to update a label that represents the local Time of a place that I am getting from a API.
The label exist in aTableView that gets loaded from a func configure() from Class WeatherCellLocationCell

I ca represent the time but I can use the Timer to make it work as current Local Time

I am not familiar with Timer() and if someone could guide me it would be great.

I now the way to use Time but I can figure out the correct one for this !

Thanks

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
            let cell = tableView.dequeueReusableCell(withIdentifier: "WeatherCellLocation", for: indexPath) as! WeatherCellLocationCell
        
            let place = fetchedResultsController.object(at: indexPath)
       // var timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(updateTime), userInfo: indexPath, repeats: true)
            cell.configureCell(weatherPlace: place)
        
            return cell

    }


class WeatherCellLocationCell: UITableViewCell {

    
    
    @IBOutlet weak var localTime: UILabel!
    @IBOutlet weak var cityName: UILabel!
    @IBOutlet weak var currentTemp: UILabel!
    

    
    func configureCell(weatherPlace : WeatherPlaceData){
        
        localTime.text = DateFormatter.localizedString(from: weatherPlace.localDate, dateStyle: .none, timeStyle: .short)
        
        cityName.text = weatherPlace.name
        let temp = Int(weatherPlace.currentTemp)
        currentTemp.text = "\(temp)"
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Hi @scy
Setting up a timer in a ‘cellForRowAt’ is not a good idea. I would suggest you to create and store your timer object in your viewcontroller. So when you receive a data from an API you create timer and in the timer callback you get a cell with the tableView.cellForRow(…) and update it’s label accordinatelly.

Nikita

This topic was automatically closed after 166 days. New replies are no longer allowed.