2017-05-27 15 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") 
相关问题