如果这是一个重复的问题,请原谅。我尽可能彻底地搜索,但是我在UITableView中看到的关于核心数据的其他问题似乎都不符合我的想法。
基本上,我有两张桌子"在我的核心数据中:
cstProjects和plProjects
这两个表都有名为" propertyOwner"和" propertyID" (其中包括),被设置为字符串。
接下来,在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 :(得分:0)
您需要在该方法中执行的第一件事是查找要显示的实例。为此,您应修改getCSTProjects
和getPLProjects
方法以保存获取请求的结果,而不是将其丢弃。您可以通过向视图控制器添加新属性来保存阵列。在相关的说明中,您真的不希望从tableView(tableView:, numberOfRowsInSection:)
调用这些方法,因为该方法可以被调用很多。在那里取物几乎可以保证性能不佳。
要获取特定实例,请在您现有的其中一个数组中查找,使用indexPath
参数获取数组索引。
拥有该特定实例后,您需要查找在这些Core Data实体类型上声明的属性值。放在哪里取决于您CSTProjects
和PLProjects
实体的定义方式以及您是否为其创建了NSManagedObject
的子类。
例如,如果您的CSTProjects
实体有一个名为name
的字符串属性,您想将其用作标题,并且您不拥有NSManagedObject
子类,您可以使用它:
cell.textLabel!.text = currentInstance.value(forKey: "name") as? String
如果你有一个NSManagedObject子类(它总是一个好主意),这将是
cell.textLabel!.text = currentInstance.name
答案 1 :(得分:0)
不是尝试将两个实体(或表)合并到一个fetchedResultsController中,而是将数据放入单个表并访问它更容易。
如果表格足够相似,您可以在具有类型代码的单个表中执行此操作,以区分您需要的条目,例如使用筛选视图使表格看起来像一种类型或另一种类型部分代码。这意味着将任何字段限制为一种类型可选,以便另一种可以忽略它们。然后frc可以使用代码字段作为分节符。
或者,如果表格大不相同,您可以添加第三个表格,比如项目,其中包含指向其他两个表的可选一对一链接,并使用填充字段的链接驱动第三个表格的frc在您的tableView中的配置单元委托方法。
除了使构建当前frc结构变得更容易之外,如果将来需求扩展,这些方法中的任何一种都会使得添加第三种或第四种类型变得更加容易,并且可以避免因将API弯曲到适合而不可避免地出现的复杂情况使用它不是为了设计的。