2017-08-09 43 views
0

我试图编写一个简单的应用程序与表视图来显示一些做的事情。 (在这种情况下,它被称为“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. 
} 

}

+0

第一个列表是否按预期显示?我的意思是,唯一的问题是更新还是从一开始就有问题?如果是这样,请发布整个代码。另一方面,如果这是你的**整个**代码,那么你的问题是你没有设置你的tableview'代理' –

+0

我几乎是一个新手,哈哈。但是这是整个代码,最后是viewDidLoad和didReceiveMemoryWarning函数,就像它们在开始时一样。 该列表显示为应该显示,并且工作正常。我可以根据需要添加和删除它。我只需要知道在应用程序重新打开时如何保存和加载数据。 :) –

+0

啊,你看,那是真正的问题(你需要更具体)。有几种方法可以处理这个问题,如果数据很小(<1 Mb),那么UserDefaults.standard是最快最简单的方法。如果不是,那么你必须考虑阅读更多关于_CoreData_或甚至使用一个'pod',例如Realm –

回答

0

好,如在注释中规定上面,你很可能会使用UserDefaults.standard在这种情况下,因为它是你处理的少量数据。如果您希望每次更新数据时都要更新数据(添加/删除新项目),那么您应该执行一些代码重构,以将保存为部分并与myTableView.reloadData()并列。这里是你如何做到这一点:

func updateData() { 
    UserDefaults.standard.set(list, forKey: "list") 
    myTableView.reloadData() 
} 

然后你就打电话updateData()到处都分别致电myTableView.reloadData()(只是让我自己清楚:更换,因为该方法已经包含了重装部分)。这就是保存部分。现在,你必须处理装载,通常你会在application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool方法,但为简单起见,我们只教你如何做这里面你已经丢失在FirstViewControllerviewDidLoad()方法做到这一点的AppDelegate类:

override func viewDidLoad() { 
    if let storedList = UserDefaults.standard.value(forKey: "list") { 
     list = storedList as! [String] 
    } else { 
     print("No list previously stored") 
    } 
} 

如果之前的list被存储,则替换该值。否则,它会将消息打印为一个确认(如果需要,您可以将其删除,以便将控制台日志保存为有用的内容)。

的最后一件事,我必须要说的是,我做了力铸造因为与storedList,因为我知道一个事实,这是一个String数组,但你要小心很小心使用as!。始终执行安全投射,因为如果失败,您的应用将崩溃。

+0

我现在感觉很愚蠢。对不起,如果我浪费你的时间,但我真的很感激它! 现在,我认为我照你所说的做了,而且我有点了解它。 但现在,没有什么是添加,当我点击我的secondViewController上的添加按钮。 –

+0

编辑帖子,显示代码:) –

+0

在你的_FirstViewController_中,一切看起来都很好,是整个_SecondViewController_代码? –