2017-08-09 45 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_代碼? –