UITableViewCell中的UserDefaults未保存(swift3)
我的代码现在只列出了您手动输入的内容。但是,当用户切换视图控制器时,代码会消失。我试图使用userdefualts将当前代码保存在选择函数中的行数中,但它不会将项保存在tableview单元格中。我只是想保存在tableview单元格中的任何东西。UITableViewCell中的UserDefaults未保存(swift3)
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var items: [String] = [""]
@IBOutlet weak var listTableView: UITableView!
@IBAction func addItem(_ sender: AnyObject) {
alert()
}
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
listTableView.dataSource = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listitem") as! ItemTableViewCell
cell.itemLabel.text = items[indexPath.row]
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let userDefaults = UserDefaults.standard
userDefaults.setValue(items, forKey: "items")
userDefaults.synchronize()
return items.count
}
func alert(){
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
alert.addTextField{
(textfield) in
textfield.placeholder = " Enter "
}
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
}
alert.addAction(add)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}}
你有一些问题在你的代码:
- 你永远不会从
UserDefaults
加载您的数据备份。 - 这是最大的 - 没有必要拨打
synchronise
。 - 您应该在添加/删除数据时保存数据,而不是在
numberOfRowsInSection
。import UIKit class ViewController: UIViewController, UITableViewDataSource { var items = [String]() @IBOutlet weak var listTableView: UITableView! @IBAction func addItem(_ sender: AnyObject) { alert() } @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() listTableView.dataSource = self self.items = UserDefaults.standard.stringArray(forKey:"items") ?? [String]() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "listitem") as! ItemTableViewCell cell.itemLabel.text = items[indexPath.row] cell.preservesSuperviewLayoutMargins = false cell.separatorInset = UIEdgeInsets.zero cell.layoutMargins = UIEdgeInsets.zero return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } func saveData() { userDefaults.standard.set(items, forKey: "items") } func alert(){ let alert = UIAlertController(title: "", message: "", preferredStyle: .alert) alert.addTextField{ (textfield) in textfield.placeholder = " Enter " } let add = UIAlertAction(title: "Add", style: .default){ (action) in guard let textfield = alert.textFields?.first else { return } if let newText= textfield.text { self.items.append(newText) saveData() let indexPath = IndexPath(row: items.count - 1, section: 0) self.listTableView.insertRows(at: [indexPath], with: .automatic) } } let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in } alert.addAction(add) alert.addAction(cancel) present(alert, animated: true, completion: nil) } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { items.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .automatic) saveData() } }
而且,你真的不应该使用
UserDefaults
用于数据存储: - 如果插入新行,而不是重新加载整个表
我建议像它看起来更好这样,但我认为这只是一个简单的学习练习。
self.items = items = UserDefaults.standard.value(forKey:“items”)as? [字符串] ?? [String]()不编译 –
对不起,这是错字。固定。 – Paulw11
请考虑使用指定方法设置[set(_:forKey:)](https://developer.apple.com/documentation/foundation/userdefaults/1414067-set)并获取[stringArray(forKey:)](https ://developer.apple.com/documentation/foundation/userdefaults/1416414-stringarray)。不要为UserDefaults建议KVC。 – nayem
当用户切换视图控制器时,代码消失是什么意思? 'numberOfRowsInSection'曾经被调用过吗? – Ryan
'items'的类型是什么?如果它是一个自定义对象,则不能将它们存储在'UserDefaults'中。 – Ryan
@Ryan当用户转到主页然后转到列表页面。所有写在列表页面上的东西在退货时都不见了。 –