Я попробовал это решение, но не повезло ... Мне нужно вернуться к ViewController и вернуться к ViewController с текстовым полем и TableView, и только тогда оно появится ... Любые другие идеи ...
од, который я до сих пор. Я могу войти в текстовое поле, чтобы оно появилось в UITableView, но только после того, как я перезагрузил страницу и вернулся к ней. Я хочу, чтобы то, что я вводил в текстовое поле, и когда я нажимал «добавить», автоматически отображалось в UITableView.
@IBOutlet var itemTextField: UITextField!
@IBOutlet var table: UITableView!
var items: [String] = []
@IBAction func add(_ sender: Any) {
let itemsObject = UserDefaults.standard.object(forKey: "items")
var items:[String]
if let tempItems = itemsObject as? [String] {
items = tempItems
items.append(itemTextField.text!)
print(items)
} else {
items = [itemTextField.text!]
}
UserDefaults.standard.set(items, forKey: "items" )
itemTextField.text = ""
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = items[indexPath.row]
cell.textLabel?.font = UIFont(name: "Type02", size: 20)
return cell
}
override func viewDidAppear(_ animated: Bool) {
let itemsOBject = UserDefaults.standard.object(forKey: "items")
if let tempItems = itemsOBject as? [String]{
items = tempItems
}
table.reloadData()
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
items.remove(at: indexPath.row)
table.reloadData()
UserDefaults.standard.set(items, forKey: "items")
}
}