[英]save tableview data in swift MVC
I have this code. 我有这个代码。 I'm developing my app without storyboard: 我正在开发没有情节提要的应用程序:
SecondController.swift SecondController.swift
class SecondController: UIViewController {
var items: [String] = ["DATA 1", "DATA 2", "DATA 3"]
let tableView: UITableView = UITableView()
init(){
super.init(nibName: nil, bundle: nil)
self.view = SecondView()
self.view.backgroundColor=UIColor.green
tableView.frame = CGRect(0, 50, 320, 200);
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
}
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func onClickListener(object : UIButton!) {
self.navigationController!.popViewController(animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}}
extension SecondController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
items.append("NEW DATA")
print(items.count)
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = self.items[indexPath.row]
return cell
}}
And SecondView.swift 和SecondView.swift
class SecondView: UIView {
var btnImage = UIButton(image: "resume")
override init(frame: CGRect){
super.init(frame: screenSize)
self.backgroundColor = UIColor.black
// Btn
self.btnImage.translatesAutoresizingMaskIntoConstraints = false
addSubview(self.btnImage)
self.btnImage.alignLeftOfViewVoid(padding: 1)
self.btnImage.addTarget(self.parentViewController, action: #selector (SecondController.onClickListener(object:)), for: .touchUpInside)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit{
self.removeFromSuperview()
}}
The table works perfectly, but when I press the button the app returns to FirstView and FirstController, and when I want return to SecondView and SecondController, I lose the new data items. 该表工作正常,但是当我按下按钮时,应用程序返回到FirstView和FirstController,并且当我想要返回SecondView和SecondController时,我丢失了新的数据项。
Where can I put the array data if I don't want lose them? 如果不想丢失数据,该将数据放在哪里? Can I separate the TableView in another file? 我可以将TableView分开在另一个文件中吗?
Thank you 谢谢
You said you go back to FirstController
, so your SecondController
gets destroyed from memory along with items
array. 您说过要回到FirstController
,所以您的SecondController
会与items
数组一起从内存中销毁。 If you want to have them accessible across the app, you should create a Model class (according to MVC pattern that you mentioned yourself in your question title). 如果要在应用程序中访问它们,则应创建一个Model类(根据您在问题标题中提到的MVC模式)。
class DataStorage {
static let shared = DataStorage()
var items: [String] = ["DATA 1", "DATA 2", "DATA 3"]
}
Now you can access this data from your SecondController
like this: 现在,您可以像这样从SecondController
访问此数据:
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return DataStorage.shared.items.count
}
This is, of course, rough implementation, and you will most likely need to implement a set of methods in DataStorage
class to modify (create, update, delete) your items
. 当然,这是粗略的实现,您很可能需要在DataStorage
类中实现一组方法来修改(创建,更新,删除) items
。
UPD : This way lets you only store data in memory until app is terminated. UPD :通过这种方式,您只能在应用程序终止之前将数据存储在内存中。 In order to persist it, you need to store it on disk using CoreData , SQLite , Property Lists etc, as described in article suggested by @f_qi. 为了持久保存它,您需要使用CoreData , SQLite , 属性列表等将其存储在磁盘上,如@f_qi建议的文章所述。 Anyway, you should have separated Model file like DataStorage
to manage this logic. 无论如何,您应该有单独的Model文件(例如DataStorage
来管理此逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.