编辑swift后保存tableview文本(Saving the tableview text after edited swift)

我已经设法创建了一个包含数组或消息的tableview,当我点击一个tableview单元格时,它会显示一个alertview和一个uitextfield,你可以更改tableview的文本。 我现在要做的是在更改时保存tableview文本(“message”)。 因为当我去另一个视图控制器并返回时它不会保存。 我是快速编程的新手,想知道如何实现这一目标。 我已经阅读了一些我需要使用nsuserdefaults但仍然不熟悉的文章。 这是我的代码如下。 多谢你们。

class Messages: UITableViewController { @IBOutlet var uitableView: UITableView! var tField: UITextField! var index: Int? var msgArray: [String]! var messages = ["Message 1", "Message 2", "Message 3", "Message 4", "Message 5", "Message 6"] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return messages.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let messagesCell = messages[indexPath.row] cell.textLabel?.text = messagesCell return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let path = tableView.indexPathForSelectedRow index = path?.row changeMessage() } func changeMessage() { let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert) alert.addTextField(configurationHandler: configurationTextField) let doneAction = UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in let modelString = self.tField.text self.messages[self.index!] = modelString! self.tableView.reloadData() }) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(doneAction) alert.addAction(cancelAction) self.present(alert, animated: true, completion: nil) } func configurationTextField(textField: UITextField!){ if (textField) != nil { tField = textField textField.text = messages[index!] } } }

I have managed to create a tableview with an array or messages and when i tapped a tableview cell it shows an alertview and a uitextfield that you can change the text of the tableview. What i'm trying to do now is to save the tableview text ("message") when it changed. Because when I go to another view controller and go back it does not save. I am new on swift programming and wondering how can i achieve that. I have read some articles that i need to use nsuserdefaults but i am not familiar with that. here is my code below. Thanks guys.

class Messages: UITableViewController { @IBOutlet var uitableView: UITableView! var tField: UITextField! var index: Int? var msgArray: [String]! var messages = ["Message 1", "Message 2", "Message 3", "Message 4", "Message 5", "Message 6"] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return messages.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let messagesCell = messages[indexPath.row] cell.textLabel?.text = messagesCell return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let path = tableView.indexPathForSelectedRow index = path?.row changeMessage() } func changeMessage() { let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert) alert.addTextField(configurationHandler: configurationTextField) let doneAction = UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in let modelString = self.tField.text self.messages[self.index!] = modelString! self.tableView.reloadData() }) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(doneAction) alert.addAction(cancelAction) self.present(alert, animated: true, completion: nil) } func configurationTextField(textField: UITextField!){ if (textField) != nil { tField = textField textField.text = messages[index!] } } }

最满意答案

尝试使用nsuserdefaults。 在SWIFT 3中

override func viewWillAppear(_ animated: Bool) { //Get the array if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { self.messages = array } } func changeMessage() { let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert) alert.addTextField(configurationHandler: configurationTextField) let doneAction = UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in let modelString = self.tField.text self.messages[self.index!] = modelString! UserDefaults.standard.set(self.messages, forKey: "messages") // Set the array with changes self.tableView.reloadData() }) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(doneAction) alert.addAction(cancelAction) self.present(alert, animated: true, completion: nil) }

Try to use nsuserdefaults. In SWIFT 3

override func viewWillAppear(_ animated: Bool) { //Get the array if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> { self.messages = array } } func changeMessage() { let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert) alert.addTextField(configurationHandler: configurationTextField) let doneAction = UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in let modelString = self.tField.text self.messages[self.index!] = modelString! UserDefaults.standard.set(self.messages, forKey: "messages") // Set the array with changes self.tableView.reloadData() }) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(doneAction) alert.addAction(cancelAction) self.present(alert, animated: true, completion: nil) }

更多推荐