2016-05-02 39 views
3

我建立使用的UITableViewController單個的ViewController應用在夫特2.動態負載的數據的UITableView迅速

我有這裏的問題是,如果我加載viewDidLoad中函數內的數據,所述的UITableView與數據顯示良好。我試圖從一個自定義的委託方法,這是在數據被保存到核心數據後調用,然後self.tableview.reloadData()UITableView將不會顯示數據中獲取此數據。

從我的調試,像 override func numberOfSectionsInTableView(tableView: UITableView) -> Int override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

來看我自定義的委託方法

func didReceiveQualifications(qualifications: [Qualification]) { 
    print("didReceivedQualifications") 
    self.qualifications = qualifications 

    // Pass qualification to view 
    self.tableView.reloadData() 

} 
+0

該代碼和安裝應實際工作......你已經添加'print'語句的兩種方法'cellForRowAtIndexPath'和'numberOfSectionsInTableView'以及看看他們什麼時候打電話?在委託方法出現之前,調用它們是非常正常的。 **但是**在執行'self.tableView.reloadData()'後應該再次調用它們。此外,您可能想要顯示'numberOfSectionsInTableView'函數的內容。 – luk2302

+0

我知道它不給你一個正確的答案,但你有沒有考慮使用NSFetchedResultsController?它爲您創建了Core數據對象和UITableViewController之間的橋樑。請參閱https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/nsfetchedresultscontroller.html – Gustanas

回答

1

可以通過依靠地方didReceiveQualifications()已經launched..in哪個線程運行前。 。

UI的所有更新都應在主線程中完成。

儘量讓reloadData主線程:

func didReceiveQualifications(qualifications: [Qualification]) { 
    print("didReceivedQualifications") 
    self.qualifications = qualifications 

    // Pass qualification to view 
    dispatch_async(dispatch_get_main_queue()) { 
     self.tableView.reloadData() 
    } 
} 
+0

不確定這是否解決了問題,但它是一個很好的觀點! – luk2302

+0

嘗試一下,如果它不工作的報告結果,我有另一種想法.. –

+0

@AlessandroOrnano,但是,如果我開始滾動UITableView,所有的數據將消失 – ankermarco