2016-02-28 26 views
0

我有TableViewControllerUITableView,視圖控制器類有一個名爲isFolder的變量。使用UITableView時未調用開關語句的情況

在整個ViewController我使用switch語句來確定我應該是使用任一selectedFolder1selectedObjects哪個變量。每個變量都是一個NSManagedObject子類,並且有一個返回數組的方法arrayOfItems

準備繼續時isFolder的值取決於傳遞的數據。

這一切工作正常。問題是數據傳遞時,我使用switch語句來填充表。當switch語句的情況是true for isFolder它不被cellForRowAtIndexPath表函數調用,但用於numberOfRowsInSection。但是,當我在解除模態視圖後重新加載視圖時,數據出現。

例如,

該函數工作正常,並且調用switch語句的情況爲true。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch isFolder { 
    case true: print("no. of cells for folder"); return self.selectedFolder.arrayOfItems.count 
    case false: print("no. of cells for object"); return self.selectedObject.arrayOfItems.count 
    } 
} 

但是下面的函數來填充tableview單元格switch語句case爲true不會被調用。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell 
    let color = colorWheel() 
    cell.selectionStyle = .None 

    // Configure the cell... 

    cell.backgroundColor = color.white 

    switch isFolder { 
    case true: 
     print("Using selected Folder") 
     cell.textLabel?.text = selectedFolder.arrayOfItems[indexPath.row].title 

    case false: 
     print("Using selected Object") 
     cell.textLabel?.text = selectedObject.arrayOfItems[indexPath.row].title 

    } 
    return cell 
} 

當案件爲假時,一切正常,數據首次顯示。如前所述,如果switch語句爲true,表格中將顯示數據,但只有在我解散模態視圖併發出通知才能調用self.table.reloadData()時纔會顯示數據。我打電話給viewDidLoad中的表格方法reloadData,但是這沒有任何作用。

有沒有人有任何建議?

+0

如果你在'tableView(_:numberOfRowsInSection)內部放置一個斷點''self.selectedFolder.arrayOfItems.count'的真值是什麼?如果它是零,'cellForRowAtIndexPath'將不會被調用 – dbn

+0

哦,對,它是0.我以爲因爲在'numberOfRows'中調用print的次數與數組的次數相同,所以應該很好。我現在調用'prepareForSegue'時重新生成了數組。 – RileyDev

回答

2

確保在調用UITableView對象上的reloadData()之前填充數據對象。否則,如果numberOfRowsInSections返回0,cellForRowAtIndexPath將不會被調用