尝试设置计时器以在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
}
嗨Reinier。您还没有在configureCell中写入currentWeatherPlaceData = weatherPlace?如果是的话,我尝试过,我仍然是我的标签没有显示时间,但只是我从API获得的时间。 –
不用担心,快速回复的方式非常感谢。我仍然得到相同的结果。计时器不起作用 –
@PS你在self.currentWeatherPlaceData.localDate中有什么和你需要显示什么?一个countDown ?,请解释一下,问题是你试图展示什么,如果你显示日期时间,即使有计时器也会显示相同的 –