ios - Tableview重新加载数据将数字恢复为默认值

标签 ios swift uitableview

我正在尝试呈现用户使用弹出窗口输入的数据(在单元格中)。我能够从弹出窗口中检索数据,但是,当我弹出另一个弹出窗口时,它会恢复为原始值。
View Controller

var highScore = "~"
var level = "1"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let scorePopupViewController = segue.destination as? ScorePopupViewController{
            scorePopupViewController.onSaveScore = updateHighScore
        }else if let levelPopupViewController = segue.destination as? LevelViewController{
            levelPopupViewController.onSaveLevelMyKey = updateLevel
            levelPopupViewController.mode = sender as! Int
        }
    }


func updateHighScore(_ data:String?)->(){
        if data != nil{
            highScore = data!
        }
        tableView.reloadData()
    }
    
func updateLevel(_ data:[String]?)->(){
        if data != nil{
             level = String(data![1])
        }
        tableView.reloadData()
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//third section
if indexPath.section == 3{
            let statisticCell:StatisticTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "STATS_CELL") as? StatisticTableViewCell
            statisticCell.delegate = self
            statisticCell.scoreSelectBtn.titleLabel?.text = self. highScore
            statisticCell.levelSelectBtn.titleLabel?.text = self.level    
            return statisticCell
}

//Delegate for cell
func scoreSelectBtnWasPressed() {
     performSegue(withIdentifier: "TO_SCOREPOPVC", sender: nil)
 }
    
func levelSelectBtnWasPressed() {
     performSegue(withIdentifier: "TO_LEVELPOPVC", sender: 0)
 }
ScorePopupVC
var onSaveScore:((_ data: String?)->())?

@IBAction func addScoreBtnWasPressed() {
        if scoreTextField.text! != ""{
            onSaveScore?(scoreTextField.text!)
        }else{
            onSaveScore?(nil)
        }
        dismiss(animated: true)
    }

@objc func dismissView (_ sender: UITapGestureRecognizer) {
        
        if scoreTextField.text! == ""{
            onSaveScore?(nil)
        }
        dismiss(animated: true)
    }

奇怪的是,当我在 cellForRowAt 中打印高分和级别时我得到正确的分数。它有点复杂,所以我创建了一个正在发生的事情的图表。任何帮助表示赞赏
enter image description here

最佳答案

您应该使用 setTitle(title: String?, for: UIControl.State) 更新 UIButton 的标题函数,而不是直接访问 titleLabel。这样, View 就会被告知 uibutton 内容需要更新。

关于ios - Tableview重新加载数据将数字恢复为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60167742/

相关文章:

iOS 5及以上版本: enable landscape mode in MPMoviePlayerController

iphone - ARC 语义问题 : No Visible @Interface for 'UIViewController' declares the selector 'loginResult:'

ios - Swift 中的 Google SingIn 获取个人资料信息

ios - 限制服务器只响应 iOS 应用程序发出的 HTTP 请求 - 没有用户名和密码

ios - 为什么我的 Table View Cell 无法显示?

iphone - 如何在不同的导航 View 中分配和初始化 NSObject?

ios - Xcode 8 语法突出显示不起作用

ios - 删除适当的图像

ios - URLSession 结果为 NIL 数据

ios - 调整自定义 UITableViewCell 及其 subview 的大小