嘗試設置計時器以在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 ?,請解釋一下,問題是你試圖展示什麼,如果你顯示日期時間,即使有計時器也會顯示相同的 –