2016-10-02 73 views
0

請原諒如果這是一個重複的問題。我儘可能徹底地搜索,但是我在UITableView中看到的有關核心數據的其他任何問題似乎完全符合我所要做的。需要幫助在Swift中的UITableView中顯示核心數據

基本上,我在我的核心數據兩個「表」:

cstProjects和plProjects

兩個表具有所謂的「propertyOwner」和「屬性ID」(等等)的屬性,被設置成字符串。

接下來,在一個ViewController中,我有一個表視圖實例,樣式設置爲Subtitle,並將單元格標識設置爲「projectCell」。

因爲我在覈心數據中有兩個表,所以我將表初始化爲有兩個部分。我希望第一部分顯示cstProjects表中的所有項目,第二部分顯示plProjects表中的所有項目。

這是我的ViewController代碼到目前爲止。我已經發表了評論,盡我所能對其進行解釋。我創建的函數可能有點矯枉過正,可以確定每個部分應該有多少「行」,但我不知道更簡單的方法。

在下面的代碼中,您看到雙重問號「??」是我不知道該放什麼東西來展示當前我們正在迭代的項目類型的當前「行」。儘管如此,如果我不得不盡可能簡化我的問題,我只需要知道如何在UITableView中顯示核心數據表的行。

import UIKit 
import CoreData 

class LoadProjectViewController: UIViewController, UITableViewDataSource, NSFetchedResultsControllerDelegate { 

    let projectSectionCount: Int = 2 
    let cstSection: Int = 0 
    let plSection: Int = 1 

    // Implement UITableViewDataSource methods 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return projectSectionCount 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     switch(section) { 
     case cstSection: 
      return getCSTProjects() 
     case plSection: 
      return getPLProjects() 
     default: 
      return 0 
     } 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("projectCell")! as UITableViewCell 

     switch (indexPath.section) { 
      case cstSection: 
       //this is where I need to set the property owner for the CST Project 
       cell.textLabel!.text = ?? 

       //this is where I need to set the project ID for the CST Project 
       cell.detailTextLabel!.text = ?? 
      case plSection: 
       //this is where I need to set the property owner for the PL Project 
       cell.textLabel!.text = ?? 

       //this is where I need to set the project ID for the PL Project 
       cell.detailTextLabel!.text = ?? 

      default: 
       cell.textLabel!.text = "Unknown" 
     } 

     return cell 
    } 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     switch section { 
      case cstSection: 
       return "Standing Timber Projects" 
      case plSection: 
       return "Purchased Logs" 
      default: 
       return "Unknown" 
     } 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func getCSTProjects() -> Int { 
     let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) 
     let context: NSManagedObjectContext = appDel.managedObjectContext 
     let request = NSFetchRequest(entityName: "CSTProjects") 
     var results = [AnyObject]() 

     request.returnsObjectsAsFaults = false 

     do { 
      results = try context.executeFetchRequest(request) 
     } catch let error { 
      print("There was an error loading the data. \(error)") 
     } 

     if (results.count > 0) { 
      return results.count 
     } else { 
      return 0 
     } 
    } 


    func getPLProjects() -> Int { 
     let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) 
     let context: NSManagedObjectContext = appDel.managedObjectContext 
     let request = NSFetchRequest(entityName: "PLProjects") 
     var results = [AnyObject]() 

     request.returnsObjectsAsFaults = false 

     do { 
      results = try context.executeFetchRequest(request) 
     } catch let error { 
      print("There was an error loading the data. \(error)") 
     } 

     if (results.count > 0) { 
      return results.count 
     } else { 
      return 0 
     } 

    } 
} 

回答

0

您需要在該方法中執行的第一件事是查找要顯示哪個實例。要做到這一點,你應該修改你的getCSTProjectsgetPLProjects方法來保存獲取請求的結果,而不是丟棄它們。你可以通過向視圖控制器添加新的屬性來保存數組。在相關說明中,您真的不想從tableView(tableView:, numberOfRowsInSection:)調用這些方法,因爲該方法可能會被調用很多。在那裏進行提取幾乎可以保證較差的性能。

要獲取特定的實例,請使用indexPath參數在現有的其中一個數組中查找它,以獲取數組索引。

一旦你有了這個特定的實例,你需要查找你在這些核心數據實體類型上聲明的屬性的值。該放什麼取決於你的CSTProjectsPLProjects實體是如何定義的以及你是否爲它們創建了NSManagedObject的子類。

例如,如果你CSTProjects實體有一個字符串屬性名爲name要作爲標題使用,你NSManagedObject子類,你可以這樣做:

cell.textLabel!.text = currentInstance.value(forKey: "name") as? String 

如果您確實有一個NSManagedObject子類(它總是一個好主意),這將是

cell.textLabel!.text = currentInstance.name 
0

,而不是試圖在兩個實體(或表)合併成一個單一fetchedResultsController,將數據放入單個表並訪問它會更容易。

如果這些表格非常相似,可以在一個帶有類型代碼的表中做到這一點,以區分需要的條目,例如使用過濾視圖使表格看起來像一種類型或另一種類型你的代碼的其他部分。這意味着將任何字段限制爲一種可選,所以另一個可以忽略它們。然後frc可以使用代碼段作爲分節符。

或者,如果表格足夠不同,則可以添加第三個表格,例如Projects,其中有可選的一對一鏈接到其他兩個表格,並使用要填充的鏈接將frc從第三個表格中取出你的tableView中的字段在配置單元格委託方法中。

除了可以更容易地構建當前的frc結構,如果您的需求在將來擴展時,這些方法中的任何一種都可以使添加第三種或第四種類型變得更加容易,並且可以避免將API彎曲到不可避免的複雜情況適合它沒有設計的用途。