2017-05-27 14 views
0

我正在製作我的第一個應用程序,它有一個任務管理器,爲此,我創建了一個表格視圖,您可以在其中通過警報輸入「任務」。我怎樣才能保存輸入的「任務」?我做了一些研究,但是,我無法找到答案。 代碼如下: (斯威夫特2)我做了一個表格視圖,即使應用程序關閉後,我如何保存這些項目?

import UIKit 

class ThirdViewController: UIViewController, UITableViewDataSource{ 

var items: [String] = [] 
@IBOutlet weak var listTableView: UITableView! 

@IBAction func additem(sender: AnyObject) { 
alert() 

} 

@IBAction func savebtn(sender: AnyObject) { 
print (items) 

} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    listTableView.dataSource = self 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("listitem") as! ItemTableViewCell 
    cell.itemLabel.text = items[indexPath.row] 
    return cell 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 

func alert() { 
    let alert = UIAlertController(title: "", message: "", preferredStyle: .Alert) 

    alert.addTextFieldWithConfigurationHandler{ 
    (textfield) in 
    textfield.placeholder = "Enter your task" 
    } 
    let add = UIAlertAction(title: "Add ", style: .Default) { 
     (action) in 
     let textfield = alert.textFields![0] 
     self.items.append(textfield.text!) 
     self.listTableView.reloadData() 
     } 
    let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { 
     (alert) in 

     print ("Hi") 
    } 
alert.addAction(add) 
alert.addAction(cancel) 

presentViewController(alert, animated: true, completion: nil) 

} 
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    items.removeAtIndex(indexPath.row) 
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

} 
+4

無關,但如果您剛剛開始使用Swift,爲什麼不使用Swift 3? Swift 2已經過時,不再支持。 – rmaddy

+0

你的問題和你的解釋沒有同步。你說應用程序關閉後你想保存。但你解釋說你不能在tableview中保存數據,哪一個是正確的? –

+0

我建議你閱讀關於核心數據並使用它;那麼你的數據將被保存爲「隨你去」 – Paulw11

回答

1

我不知道我是否正確理解你的問題,但我認爲這可能有助於蘋果的教程閱讀並遵循「堅持數據「部分。

(鏈接:https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/PersistData.html#//apple_ref/doc/uid/TP40015214-CH14-SW1

這是一步一步的教程附帶詳細的解釋。應該能夠爲您提供一個如何爲簡單應用程序保存數據的好主意。 (如果需要更大的數據庫,核心數據是值得閱讀。)

1

你可以將任何內容的本地或雲中,

地方 - >核心數據或領域

雲 - >您的自定義API(顯然您還沒有準備好)或Cloud API(如Firebase,Backendless)

請觀看一些教程,爲您的目的和未來的應用程序選擇一個教程。

祝你好運。

+0

也是他可以使用[userDefaults](https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults)。但是這是爲了節省小的簡單的東西。對於真正的持久性Core Data或Realm要好得多 – Honey

0
The easiest way to Persist Data or store data even after your application is terminated or closed is use RMMapper,it work like nsuserdefaults but by using RMMapper we can store whole object.. 
//set 
UserDefaults.standard.rm_setCustomObject(Obj1, forKey: "myObject") 

//retrive 

let obj = UserDefaults.standard.rm_customObject(forKey: "myObject") 
相關問題