最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Unable to reset user default text within a set cell: - Stack Overflow

programmeradmin4浏览0评论

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

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()
}
Share Improve this question edited Mar 25 at 2:05 HangarRash 15.1k5 gold badges20 silver badges55 bronze badges asked Mar 25 at 1:30 Train LoverTrain Lover 199 bronze badges 9
  • Just override your WeatherCell prepareForReuse method and set those values there and reload – Leo Dabus Commented Mar 25 at 1:45
  • Hay @LeoDabus, how would I do that, sorry I'm a bit unfamiliar with doing that? would you like me to add any further information to help with explaining that?
发布评论

评论列表(0)

  1. 暂无评论