gpt4 book ai didi

arrays - 如何保存数组。所以App重启后也是一样

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:13 24 4
gpt4 key购买 nike

我想知道如何让我的待办事项在应用程序关闭后保持不变。在我的代码中,我一开始就声明了一个数组,我想知道如何在用户重新启动应用程序后为用户保存更新的数组。我在网上搜索了一些答案,但只找到了关于在 Storyboard 中创建实体的旧想法。而且我很确定,到现在为止必须有比手动添加更漂亮的东西。

如何在数组更新时保存数组,以使其在应用重启后不会丢失?

我的代码:

import UIKit

var checklist = ["Item 1", "Item 2"]

class ChecklistViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{

var newChecklistItemString: String?
var alertInputTextField: UITextField?

@IBOutlet weak var myTableView: UITableView!

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var selectedChecklist: [String] = []

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return (checklist.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
cell.textLabel?.text = checklist[indexPath.row]

if selectedChecklist.contains(checklist[indexPath.row]) {
cell.accessoryType = .checkmark
}
else{
cell.accessoryType = .none
}

return cell
}

// checkmarks when tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
selectedChecklist.append(checklist[indexPath.row])
tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let value = checklist.remove(at: indexPath.row)
myTableView.reloadData()
}
}

override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}

override func viewDidLoad() {
super.viewDidLoad()

addSlideMenuButton()
}

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

@IBAction func addNewObject(_ sender: Any) {

let alert = UIAlertController(title: "New Item", message: nil, preferredStyle: .alert)
alert.addTextField { (alertInputTextField) in
alertInputTextField.autocapitalizationType = .sentences
}

alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: "Add", style: .default, handler: { (action) in

let textf = alert.textFields![0] as UITextField

if(textf.text != "")
{
checklist.append(textf.text!)
}
self.myTableView.reloadData()

}))

self.present(alert, animated: true, completion: nil)

}
}

最佳答案

对于简单的字符串数组,使用 UserDefaults

  • 声明两个加载和保存数据的方法

    func loadDefaults()
    {
    self.checklist = UserDefaults.standard.array(forKey: "checklist") as? [String] ?? []
    }

    func saveDefaults()
    {
    UserDefaults.standard.set(self.checklist, forKey: "checklist")
    }
  • viewDidLoad中添加

    loadDefaults()
  • 在添加和删除项目后立即添加

    saveDefaults()

对于更复杂的数据,其他解决方案(如 Core Data)更可取


注意:

与其总是重新加载整个 TableView ,不如使用 API 来插入和删除单行。好处是一个漂亮的动画。

tableView:commit 中将 myTableView.reloadData() 替换为

myTableView.deleteRows(at: [indexPath], with: .automatic)

并在警报操作中将整个 if 表达式替换为

if !textf.text!.isEmpty {
let indexPath = IndexPath(row: checklist.count, section: 0)
checklist.append(textf.text!)
myTableView.insertRows(at: [indexPath], with: .automatic)
}

并将变量 checklist 移到类中!

关于arrays - 如何保存数组。所以App重启后也是一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com