ios - 快速编辑后保存表格 View 文本

标签 ios swift uitableview swift3

我已经设法创建了一个带有数组或消息的表格 View ,当我点击一个表格 View 单元格时,它会显示一个警报 View 和一个可以更改表格 View 文本的 uitextfield。我现在要做的是在更改时保存表格 View 文本(“消息”)。因为当我转到另一个 View Controller 并返回时,它不会保存。我是快速编程的新手,想知道如何实现这一目标。我已经阅读了一些我需要使用 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!]
        }
    }
}

最佳答案

尝试使用 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)
}

关于ios - 快速编辑后保存表格 View 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41948748/

相关文章:

ios - Swift -Firebase 从 snapshot.children 的子集中获取最后一个 childKey

ios - 从一个函数访问另一个 Swift 函数的变量

iOS UITableView CheckMark 出现在每一行

ios - Xamarin 无法在 iPhone 模拟器上运行 iOS 应用程序 - 不支持 386 架构

ios - 在 iOS 和 macOS 之间编码和解码 Int 值

带有 key 的 Java HmacSHA256

iphone - 自定义 UITableViewCell 未使用 .xib(很可能是因为 init 方法中的缺陷)

ios - 将第三方框架链接到我自己的框架,使用 Carthage 进行分发

ios - TableViewCell 中的 Swift 3 UISwitch 在滚动时丢失状态

ios - 在 UITableView 中从网络预加载数据