2016-10-22 49 views
0
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?感谢无法显示在tableview中的核心数据斯威夫特

+0

不,我可以构建应用程序并进行模拟,但只显示任何内容 –

回答

0

两个大错误:

  1. 您不能创建UITableViewCell()你出列成为默认初始化一个细胞。
  2. 您必须获取索引路径的数据源数组中的项目,并将属性的值分配给单元的标签。

    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是在界面生成器指定的标识符。
<nameOfProperty>是您数据模型中的一个属性。