删除不起作用,最后一个值不显示,用户默认使用 tableview

Delete not working and last value not display, User default with tableview

我实现了使用具有 table 视图的用户默认值在本地保存数据。插入数据时,每个数据都显示在我的 table 视图中。但是停止并且 运行 最后一个值不会被显示。并且当下次应用 运行 时滑动和删除不起作用。

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() 方法

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

正在调用 storeData 方法

    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

之后永远不会调用 reloadData
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()
    }
}

并使用UserDefaults的专用API(不要调用synchronize

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


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