我试图编写一个简单的应用程序与表视图来显示一些做的事情。 (在这种情况下,它被称为“Grails”/我想购买的东西)。如何保存并重新加载列表?
我做了关于表视图和输入等的所有事情,而且我唯一需要做的就是再次保存并加载所有信息。但我不知道如何。
这是我的第一个视图控制器代码:
import UIKit
var list = ["Nike Air Jordan 1 x Off White", "Balenciaga Speed Trainers", "Gucci Snake Sneakers", "Goyard Card Holder"]
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
list.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
}
而第二个视图控制器看起来是这样的:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var input: UITextField!
@IBAction func addItem(_ sender: Any) {
if (input.text != "") {
list.append(input.text!)
input.text = ""
}
}
}
编辑:
现在的代码如下像这样,但我不能从第二个视图控制器上的按钮添加任何新项目到列表中?
进口的UIKit
VAR名单= [ “耐克的Air Jordan 1个关白”, “巴黎世家飞车培训师”, “古奇蛇运动鞋”, “戈雅持卡人”]
类FirstViewController:的UIViewController ,的UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (list.count)
}
func updateData() {
UserDefaults.standard.set(list, forKey: "list")
myTableView.reloadData()
}
//LOADING THE DATA AGAIN
override func viewDidLoad() {
if let storedList = UserDefaults.standard.value(forKey: "list")
{
list = storedList as! [String]
}
else
{
print("No list previously stored")
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
{
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
{
let item = list[sourceIndexPath.row]
list.remove(at: sourceIndexPath.row)
list.insert(item, at: destinationIndexPath.row)
}
@IBAction func edit(_ sender: Any)
{
myTableView.isEditing = !myTableView.isEditing
switch myTableView.isEditing {
case true:
editButtonItem.title = "Done"
case false:
editButtonItem.title = "Edit"
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == UITableViewCellEditingStyle.delete
{
list.remove(at: indexPath.row)
updateData()
}
}
override func viewDidAppear(_ animated: Bool) {
updateData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
第一个列表是否按预期显示?我的意思是,唯一的问题是更新还是从一开始就有问题?如果是这样,请发布整个代码。另一方面,如果这是你的**整个**代码,那么你的问题是你没有设置你的tableview'代理' –
我几乎是一个新手,哈哈。但是这是整个代码,最后是viewDidLoad和didReceiveMemoryWarning函数,就像它们在开始时一样。 该列表显示为应该显示,并且工作正常。我可以根据需要添加和删除它。我只需要知道在应用程序重新打开时如何保存和加载数据。 :) –
啊,你看,那是真正的问题(你需要更具体)。有几种方法可以处理这个问题,如果数据很小(<1 Mb),那么UserDefaults.standard是最快最简单的方法。如果不是,那么你必须考虑阅读更多关于_CoreData_或甚至使用一个'pod',例如Realm –