简体   繁体   中英

Use Timer to set Time in UILabel In UITableViewCell

Trying to set a timer to present time on a label inside a tableViewCell.

Because of my lack of knowledge I am facing this problem.

I am familiar with Timer but I can figure out the most efficient way for this one.

Thanks a lot

Here is my code

import UIKit



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)

    }

}

here I am loading the tableView

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

    }

First add the WeatherPlaceData object in your cell and put the timer in your cell

var currentWeatherPlaceData: WeatherPlaceData?
fileprivate var timeWorking : Bool = false
var timer:Timer?

Add a method to update your label

func updateTime()
    {
       localTime.text = DateFormatter.localizedString(from: self.currentWeatherPlaceData.localDate, dateStyle: .none, timeStyle: .short)
    }

Change your configureCell method to this one

func configureCell(weatherPlace : WeatherPlaceData){
        self.currentWeatherPlaceData = weatherPlace
        if(!timeWorking)
        {
        localTime.text = DateFormatter.localizedString(from: self.currentWeatherPlaceData.localDate, dateStyle: .none, timeStyle: .short)
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
        self.timeWorking = true
        }

        cityName.text = weatherPlace.name
        let temp = Int(weatherPlace.currentTemp)
        currentTemp.text = "\(temp)"

    }

And finally adjust the prepare for reuse to avoid issues, restarting your timer

override func prepareForReuse() {
    super.prepareForReuse()
    self.timer.invalidate()
    self.timeWorking = false
}

Hope this helps

I would add a Timer to the cell itself that recalculates the time every second.

class WeatherCellLocationCell: UITableViewCell {

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

var timer: Timer?
var weatherPlace: WeatherPlaceData?

func configureCell(weatherPlace : WeatherPlaceData){
    self.weatherPlace = weatherPlace
    setTime()
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(WeatherCellLocationCell.setTime), userInfo: nil, repeats: true)
}

func setTime() {
    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)
}

override func prepareForReuse() {
    if timer != nil {
        timer?.invalidate()
        timer = nil
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM