【发布时间】:2018-01-29 06:48:29
【问题描述】:
尝试学习做todolist;我有两个视图控制器,一个用于存储,一个用于在表格视图中显示。我的桌子返回零;我哪里错了?
存储视图:
var toDoStorage = UserDefaults.standard.object(forKey: "toDo")
var toDoList = [String] ()
@IBAction func saveButton(_ sender: Any) {
if (toDoStorage as? [String]) != nil {
toDoList.append(itemLabel.text!)
} else {
toDoList = [itemLabel.text!]
}
UserDefaults.standard.set(toDoList, forKey: "todo")
itemLabel.text = ""
}
显示视图:
var toDo = [UserDefaults.standard.object(forKey: "toDo")]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toDo.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "IDCell")
cell.textLabel?.text = String(describing: toDo[indexPath.row])
return cell
}
【问题讨论】:
标签: arrays swift uitableview nsuserdefaults