无法在tableview Swift中显示核心数据

class showPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var records : [Record] = [] func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return records.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() return cell } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext if editingStyle == .delete{ let record = records[indexPath.row] context.delete(record) (UIApplication.shared.delegate as! AppDelegate).saveContext() do{ records = try context.fetch(Record.fetchRequest()) } catch{ print("Failed") } } } override func viewWillAppear(_ animated: Bool) { getData() tableView.reloadData() } func getData(){ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext do{ records = try context.fetch(Record.fetchRequest()) } catch{ print("123") } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

大家好,我只是试图显示核心数据在表视图中,我已经连接的dataSourcedelegateViewController ,我确认核心数据中有一些数据,任何人都可以帮我PLZ? 谢谢

两个大错误:

  1. 您不能使用默认初始化程序UITableViewCell()创build一个单元,您必须将其退出。
  2. 您必须获取索引path的数据源数组中的项目,并将属性的值分配给单元格的标签。

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let record = records[indexPath.row] cell.textLabel!.text = record.<nameOfProperty> return cell } 

cell是Interface Builder中指定的标识符。
<nameOfProperty>是数据模型中的一个属性。