我正在尝试呈现用户使用弹出窗口输入的数据(在单元格中)。我能够从弹出窗口中检索数据,但是,当我弹出另一个弹出窗口时,它会恢复为原始值。
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
中打印高分和级别时我得到正确的分数。它有点复杂,所以我创建了一个正在发生的事情的图表。任何帮助表示赞赏最佳答案
您应该使用 setTitle(title: String?, for: UIControl.State) 更新 UIButton 的标题函数,而不是直接访问 titleLabel。这样, View 就会被告知 uibutton 内容需要更新。
关于ios - Tableview重新加载数据将数字恢复为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60167742/