This is my code:
class WeatherFeatures: UIViewController, UITextViewDelegate, WeatherManagerDelegate {
let date1ForTableView = UserDefaults.standard
let low1ForTableView = UserDefaults.standard
let high1ForTableView = UserDefaults.standard
...
private lazy var WeatherForecastCells: [WeatherCell] = [
WeatherCell(image: "", date: date1ForTableView.string(forKey: "Date1")!, low: Int(low1ForTableView.double(forKey: "Low1")), high: Int(high1ForTableView.double(forKey: "High1"))),
]
}
extension WeatherFeatures: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return WeatherForecastCells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "WeatherCellsForWeatherFeature", for: indexPath) as! WeatherForecastTableViewCell
cell.WeatherDetails.text = "\(WeatherForecastCells[indexPath.row].date) - Low: \(WeatherForecastCells[indexPath.row].low) High: \(WeatherForecastCells[indexPath.row].high)"
return cell
}
}
The line that states "cell.WeatherDetails.text = "\(WeatherForecastCells[indexPath.row].date) - Low: \(WeatherForecastCells[indexPath.row].low) High: \(WeatherForecastCells[indexPath.row].high)"
" once the cell is established changes as the user interacts with the interface such as when the user changes the location they want to see the weather of. The data in the user default values changes as this is done.
My question is that is there a UIButton action that resets the cells specifically to factor in the new edits to the user default values? such as for example:
@IBAction func resetMyCells(_ sender: UIButton) {
tableview.cells.UIView.Reset()
}
Okay guys, the issue was that the User Default data was hardcoded. When -
private lazy var WeatherForecastCells: [WeatherCell] = [
WeatherCell(image: "", date: date1ForTableView.string(forKey: "Date1")!, low: Int(low1ForTableView.double(forKey: "Low1")), high: Int(high1ForTableView.double(forKey: "High1"))),
]
was called, the user default data was already entered. I simply had to move it to my cell data so that it would always be reset when the cells were entered. This was the only edit I made as well as deleting the above line of code and moving it into the cell:
//MARK: - TableViewDataSource
extension WeatherFeatures: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "WeatherCellsForWeatherFeature", for: indexPath) as! WeatherForecastTableViewCell
cell.layer.borderWidth = 1
cell.layer.borderColor = CGColor(red: 0, green: 0, blue: 0, alpha: 1)
lazy var WeatherForecastCells: [WeatherCell] = [
WeatherCell(image: "", date: date1ForTableView.string(forKey: "Date1")!, low: Int(low1ForTableView.double(forKey: "Low1")), high: Int(high1ForTableView.double(forKey: "High1"))),
]
cell.WeatherDetails.text = "\(WeatherForecastCells[indexPath.row].date) - Low: \(WeatherForecastCells[indexPath.row].low) High: \(WeatherForecastCells[indexPath.row].high)"
return cell
}
}