2016-10-22 66 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>是您數據模型中的一個屬性。