Swift - reloadData()不起作用

时间:2017-06-28 00:02:02

标签: ios swift uitableview

我正在尝试使用Xcode 3和Swift开发一个笔记应用程序。这是我的代码:

@JsonProperty

给我一​​个问题的一行是import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var table: UITableView! var data: [String] = [] override func viewDidLoad() { super.viewDidLoad() self.title = "Notes" // Add a button (add or edit) let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNote)) self.navigationItem.rightBarButtonItem = addButton self.navigationItem.leftBarButtonItem = editButtonItem load() } // function that says what happens when button is pushed func addNote() { if table.isEditing { return } let name: String = "Row \(data.count + 1)" data.insert(name, at: 0) let indexPath: IndexPath = IndexPath(row: 0, section: 0) table.insertRows(at: [indexPath], with: .automatic) self.performSegue(withIdentifier: "detail", sender: nil) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! cell.textLabel?.text = data[indexPath.row] return cell } // to set what happens when push edit button override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) table.setEditing(editing, animated: animated) } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { data.remove(at: indexPath.row) table.deleteRows(at: [indexPath], with: .fade) save() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "detail", sender: nil) } func save() { UserDefaults.standard.set(data, forKey: "notes") UserDefaults.standard.synchronize() } func load() { if let loadedData = UserDefaults.standard.value(forKey: "notes") as? [String] { data = loadedData table.reloadData() } } 。该应用程序在模拟器上弹出,但没有内容,表格也没有显示。我错过了什么?

0 个答案:

没有答案