【发布时间】:2016-10-02 20:12:52
【问题描述】:
如果这是一个重复的问题,请原谅。我尽我所能进行了彻底的搜索,但我在 UITableView 中看到的有关核心数据的其他问题似乎都不太适合我正在尝试做的事情。
基本上,我的核心数据中有两个“表”:
cstProjects 和 plProjects
两个表都有名为“propertyOwner”和“propertyID”(以及其他)的属性,它们被设置为字符串。
接下来,在 ViewController 中,我有一个表格视图实例,样式设置为 Subtitle,单元格标识符设置为“projectCell”。
因为我的核心数据中有两个表,所以我将表初始化为有两个部分。我希望第一部分显示 cstProjects 表中的所有项目,第二部分显示 plProjects 表中的所有项目。
到目前为止,这是我的 ViewController 代码。我已经把 cmets 放进去,尽我所能解释它。我创建的函数可能有点过分计算每个部分应该有多少“行”,但我不知道更简单的方法。
在下面的代码中,您可以看到双问号“??”是我不确定要放什么来显示我们当前正在迭代的项目类型的当前“行”的地方。
但最终,如果我必须尽可能简化我的问题,我只需要知道如何在 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
}
}
}
【问题讨论】:
标签: uitableview core-data swift2