【发布时间】:2018-01-17 22:10:04
【问题描述】:
我正在尝试编写一个带有表格视图的简单应用程序,以显示一些要做的事情。 (在这种情况下,它被称为“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 list = [“Nike Air Jordan 1 x Off White”、“Balenciaga Speed Trainers”、“Gucci Snake Sneakers”、“Goyard Card Holder”]
类 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
delegate -
我几乎是个新手,啊哈。但这是完整的代码,然后是最后的 viewDidLoad 和 didReceiveMemoryWarning 函数,就像它们在开始时一样。该列表按应有的方式显示,并且运行良好。我可以根据需要添加和删除它。我只需要知道如何在应用重新打开时保存和加载数据。 :)
-
啊,你看,那才是真正的问题(你需要更具体)。有几种方法可以解决这个问题,如果您的数据很小(UserDefaults.standard 是最快和最简单的方法。如果不是,那么您必须考虑阅读有关 CoreData 的更多信息,甚至使用
pod来解决它,例如 Realm -
谢谢!但是我的问题是;在这种情况下如何使用 UserDefaults?
-
给我几分钟,我会在下面回复一个格式化的回复。