我目前正在尝试将不同的实体从我的CoreData模型加载到一个UITableView中但在不同的部分下。我尝试过以下方法:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
var rows = 0
if section == 0 {
rows = crew.count
} else if section == 1 {
rows = aircraft.count
} else if section == 2 {
rows = batteries.count
}
return rows
}
但这似乎不起作用。它返回每个部分的第一个crew.count。我应该注意到,机组人员,飞机和电池都是NSManagedObject的数组。
有没有人对如何实施我正在寻找的内容有任何建议?
感谢。
编辑:以下是我为实现清晰而实施的其他方法。
var crew = [NSManagedObject]()
var aircraft = [NSManagedObject]()
var batteries = [NSManagedObject]()
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let crewFetchRequest = NSFetchRequest(entityName:"Crew")
var crewError: NSError?
let crewFetchedResults = managedContext.executeFetchRequest(crewFetchRequest, error: &crewError) as? [NSManagedObject]
if let crewResults = crewFetchedResults {
crew = crewResults
} else {
println("Could not fetch \(crewError), \(crewError!.userInfo)")
}
let aircraftFetchRequest = NSFetchRequest(entityName:"Aircraft")
var aircraftError: NSError?
let aircraftFetchedResults = managedContext.executeFetchRequest(aircraftFetchRequest, error: &aircraftError) as? [NSManagedObject]
if let aircraftResults = crewFetchedResults {
aircraft = aircraftResults
} else {
println("Could not fetch \(aircraftError), \(aircraftError!.userInfo)")
}
let batteryFetchRequest = NSFetchRequest(entityName:"Battery")
var batteryError: NSError?
let batteryFetchedResults = managedContext.executeFetchRequest(batteryFetchRequest, error: &batteryError) as? [NSManagedObject]
if let batteryResults = crewFetchedResults {
batteries = batteryResults
} else {
println("Could not fetch \(batteryError), \(batteryError!.userInfo)")
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
var rows = 0
if section == 0 {
rows = crew.count
} else if section == 1 {
rows = aircraft.count
} else if section == 2 {
rows = batteries.count
}
return rows
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return sectionTitles[section]
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("CrewCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
switch indexPath.section.description
{
case "0":
cell.textLabel!.text = crew[indexPath.item].valueForKey("name") as? String
if crew[indexPath.item].valueForKey("pilot") as? Bool == true {
cell.detailTextLabel!.text = "Pilot"
} else {
cell.detailTextLabel!.text = " "
}
break;
case "1":
cell.textLabel!.text = "Aircraft"
break;
case "2":
cell.textLabel!.text = "Battery"
break;
default:
break;
}
return cell
}
答案 0 :(得分:1)
您的代码很好,但您需要通过实施
来设置tableview中的部分数量override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
编辑:
从编辑过的问题中,我现在可以清楚地看到您的问题与数据源有关
let aircraftFetchRequest = NSFetchRequest(entityName:"Aircraft")
var aircraftError: NSError?
let aircraftFetchedResults = managedContext.executeFetchRequest(aircraftFetchRequest, error: &aircraftError) as? [NSManagedObject]
if let aircraftResults = crewFetchedResults {
aircraft = aircraftResults
} else {
println("Could not fetch \(aircraftError), \(aircraftError!.userInfo)")
}
let batteryFetchRequest = NSFetchRequest(entityName:"Battery")
var batteryError: NSError?
let batteryFetchedResults = managedContext.executeFetchRequest(batteryFetchRequest, error: &batteryError) as? [NSManagedObject]
if let batteryResults = crewFetchedResults {
batteries = batteryResults
}
if let aircraftResults = crewFetchedResults
和if let batteryResults = crewFetchedResults
将返回crewFetchResults,因此所有工作人员,飞机和电池阵列都保持完全相同的元素。您必须使用if let aircraftResults = aircraftFetchedResults
和if let batteryResults = batteryFetchedResults
替换这些行。希望我足够清楚