簡體   English   中英

從后台關閉應用程序后如何保存已刪除的行?

[英]How to Save deleted rows after closing application from background?

我正在嘗試創建一個 TODO 列表應用程序。我將用戶輸入保存在 UserDefaults 中,並在我從主頁按鈕關閉應用程序時工作。 但是當我從后台應用程序中刪除應用程序時,我之前刪除的所有內容又回來了,這里有一些代碼:code for delete row from tableView

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        items.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

在 GUI 上編輯的代碼

DispatchQueue.main.async {
    self.items.append(text)
    var currentEntry = UserDefaults.standard.stringArray(forKey: "items") ?? []
    currentEntry.append(text)
    UserDefaults.standard.setValue(currentEntry, forKey: "items")
    self.tableView.reloadData()
}

從我在這里看到的,對我來說,當您使用 append 時,您似乎將項目 stringArray 保存到 UserDefaults,但是當您刪除條目時,您並沒有將其保存到 UserDefaults。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM