按下“保存”按钮后无法更新视图,始终需要重置应用程序

问题描述

嘿,已经有好几天尝试将其制作为应用程序了,而且还处于创建应用程序的最后。但是,按保存后,viewDidAppear()似乎没有更新视图。另外ViewTable.reloadData()也需要一个?应用程序的可选err问号,在尝试打开包装时不会给我一个意外找到的nil。长话短说,只是尝试使应用程序更新视图而不必重置应用程序。也确实希望此应用程序在一个视图控制器中运行。任何帮助将不胜感激!


import UIKit

class ViewController: UIViewController,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource {

var listedCells: [String] = []



override func viewDidLoad() {
   super.viewDidLoad()
}

override func viewDidAppear(_ animated: Bool) {
  let savedTodo = UserDefaults.standard.object(forKey: "ThingAdded")
  if let savedToList = savedTodo as? [String] {
     listedCells = savedToList;
     self.ViewTable?.reloadData();
  }

  textField?.clearButtonMode = .always;
  textField?.clearButtonMode = .whileEditing;
  ViewTable?.delegate = self;
  ViewTable?.dataSource = self;
}

internal func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return listedCells.count
}

func tableView(_ tableView: UITableView,commit deleteSlider: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {

  if deleteSlider == UITableViewCell.EditingStyle.delete {
     listedCells.remove(at: indexPath.row)
     self.ViewTable.reloadData();
     UserDefaults.standard.set(listedCells,forKey: "ThingAdded")
  }
}

internal func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cells = UITableViewCell(style: UITableViewCell.CellStyle.default,reuseIdentifier: "TDList")
    cells.textLabel?.text = listedCells[indexPath.row]
    return cells
}


@IBOutlet weak var ViewTable: UITableView!
@IBOutlet weak var textField: UITextField!
@IBAction func saveButton(_ sender: Any?) {
    let savedTodo = UserDefaults.standard.object(forKey: "ThingAdded")
    var listedCellss: [String];
    if let savedToList = savedTodo as? [String] {
         listedCellss = savedToList;
         listedCellss.append(textField.text!);
         self.ViewTable?.reloadData();
    } else {
         listedCellss = [textField.text!];
         self.ViewTable?.reloadData();
    }
    UserDefaults.standard.set(listedCellss,forKey: "ThingAdded")
}



override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
   self.view.endEditing(true);
}


public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;
 }
 }
}    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)