2017-08-05 99 views
1

尝试设置计时器以在tableViewCell中的标签上显示时间。使用计时器在UITableViewCell中设置UILabel中的时间

由于我缺乏知识,我正面临着这个问题。

我熟悉Timer,但我可以找出最有效的方法。

非常感谢

这里是我的代码

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) 

    } 

} 

我在这里装载的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 

    } 

回答

0

首先添加WeatherPlaceData对象在你的细胞,并把计时器在你的细胞

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

添加一个方法来更新您的标签

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

更改configureCell方法对这一

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)" 

    } 

最后调整准备重用,以避免问题,重新启动定时器

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

希望这会有所帮助

+0

嗨Reinier。您还没有在configureCell中写入currentWeatherPlaceData = weatherPlace?如果是的话,我尝试过,我仍然是我的标签没有显示时间,但只是我从API获得的时间。 –

+0

不用担心,快速回复的方式非常感谢。我仍然得到相同的结果。计时器不起作用 –

+0

@PS你在self.currentWeatherPlaceData.localDate中有什么和你需要显示什么?一个countDown ?,请解释一下,问题是你试图展示什么,如果你显示日期时间,即使有计时器也会显示相同的 –

0

我会添加一个计时器到单元本身,每秒重新计算一次。

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 
    } 
} 

}

+0

Hi Rool。谢谢回复。我尝试了你的代码,并得到和以前一样的结果。标签不代表时间,只是API结果日期的静态标签。 –