Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 删除不工作和最后一个值不显示,用户默认为tableview_Ios_Swift_Nsuserdefaults - Fatal编程技术网

Ios 删除不工作和最后一个值不显示,用户默认为tableview

Ios 删除不工作和最后一个值不显示,用户默认为tableview,ios,swift,nsuserdefaults,Ios,Swift,Nsuserdefaults,我使用带有表视图的用户dafaults实现了在本地保存数据。插入数据时,每个数据都显示在我的tableview中。但停止并再次运行最后一个值未显示。当下次运行应用程序时,刷卡和删除不起作用 import UIKit let defaults = UserDefaults(suiteName: "com.saving.data") class HomeWorkViewController: UITableViewController { var rows = [String]() 在

我使用带有表视图的用户dafaults实现了在本地保存数据。插入数据时,每个数据都显示在我的tableview中。但停止并再次运行最后一个值未显示。当下次运行应用程序时,刷卡和删除不起作用

import UIKit
let defaults = UserDefaults(suiteName: "com.saving.data")

class HomeWorkViewController: UITableViewController {

    var rows = [String]()
在viewDidload中调用getData()方法

    override func viewDidLoad() {
        super.viewDidLoad()
        getData()
        // Do any additional setup after loading the view.
        self.navigationItem.rightBarButtonItem = self.editButtonItem
    }
调用getData()方法

调用storeData方法

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        getData()
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(true)
        storeData()
    }

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

    @IBAction func addButton(_ sender: Any) {
        addCell()
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "homeWork", for: indexPath)
        cell.textLabel?.text = rows[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            rows.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            tableView.reloadData()
        }else if editingStyle == .insert {

        }
    }

    func addCell(){
        let alert = UIAlertController(title: "Add Home Work", message: "Input text", preferredStyle: .alert)
        alert.addTextField{(textField) in
    textField.placeholder = "text...."
        }
        alert.addAction(UIAlertAction(title: "Confirm", style: .default, handler: {[weak alert](_) in
            let row = alert?.textFields![0]
            self.rows.append((row?.text)!)
            self.tableView.reloadData()
        }))
        self.present(alert,animated: true, completion: nil)
        storeData()
    }

    func storeData(){
        defaults?.set(rows, forKey: "savedData")
        defaults?.synchronize()
    }


    func getData(){
     let data = defaults?.value(forKey: "savedData")
        if data != nil {
            rows = data as! [String]
        }else{}
    }
}
您在错误的位置调用了
storeData()
addAction
闭包将在稍后执行

func addCell() {
    let alert = UIAlertController(title: "Add Home Work", message: "Input text", preferredStyle: .alert)
    alert.addTextField{(textField) in
        textField.placeholder = "text...."
    }
    alert.addAction(UIAlertAction(title: "Confirm", style: .default, handler: {[weak alert](_) in
        let row = alert?.textFields![0]
        let insertionIndex = self.rows.count
        self.rows.append(row.text!)
        self.tableView.insertRows(at: IndexPath(row: insertionIndex, section: 0), with: .automatic)
        self.storeData()
    }))
    self.present(alert,animated: true, completion: nil)
}
并且在调用
deleteRows

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        rows.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        self.storeData()
    }
}
并使用专用的API
UserDefaults
(不要调用
synchronize


谢谢拯救正在起作用。但删除不起作用。我删除了一些值并重新运行应用程序。显示所有值。当然,您也必须调用
storeData
。我更新了答案。
func storeData(){
    defaults!.set(rows, forKey: "savedData")
}


func getData(){
    rows = defaults!.array(forKey: "savedData") as? [String] ?? []
}