gpt4 book ai didi

UITableViewController - 每个部分的单元格数量不同

转载 作者:行者123 更新时间:2023-11-30 10:15:29 26 4
gpt4 key购买 nike

我目前正在尝试将不同的实体从我的 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
}

但这似乎不起作用。它返回每个部分的第一个船员计数。我应该注意到,机组人员、飞机和电池都是 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
}

最佳答案

您的代码很好,但您还需要通过实现来设置 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 heavenResults =船员FetchedResultsif let BatteryResults =船员FetchedResults 将返回crewFetchResults,因此所有机组人员、飞机和电池数组都持有完全相同的元素。您必须将这些行替换为 if let heavenResults = heavenFetchedResultsif let BatteryResults = BatteryFetchedResults。希望我说得足够清楚

关于UITableViewController - 每个部分的单元格数量不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30263577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com