I have a tableView and it's split into 2 sections.
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
Each section has it's own FetchResultController (FRC). This is my CellForRoatAt function
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "personCell", for: indexPath) as! PersonTableViewCell
switch(indexPath.section) {
case 0:
print("this is section 0")
let person = positiveFetchedResultsController.object(at: indexPath)
cell.personName.text = person.name
//print("(person.name!) is the name")
//print("(person.statement!.count) postive person statement count")
if(person.statement!.count == 0){
print("default")
cell.statementAmount.text = "$0.00"
}
else{
print("(person.name!) has (person.statement!.count) statement count")
let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
print("(amountTotal!) this is the total")
cell.statementAmount.text = String(describing: amountTotal!)
}
case 1:
print("this is section 1")
print("(negativeFetchedResultsController.object(at: indexPath)) objects fetched")
let person = negativeFetchedResultsController.object(at: indexPath)
cell.personName.text = person.name
print("(person.name!) is the name")
print("(person.statement!.count) negative person statement count")
if(person.statement!.count == 0){
cell.statementAmount.text = "$0.00"
}
else{
print("(person.name!) has (person.statement!.count) statement count")
let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
print("(amountTotal!) this is the total")
cell.statementAmount.text = String(describing: amountTotal!)
}
default: cell.personName.text = "hello"
}
This first section 0 works, like it should and I get the correct data back from the print calls. But section 1 throws me this error and crashes my app.
error: NSFetchedResultsController: no section at index 1 in sections list
CoreData: error: NSFetchedResultsController: no section at index 1 in sections list
When I print out the indexPath I get [0,0] for section 1 and [1,0] for section 1. I would think since I'm using 2 different FRCs for each section that the indexPath would start at [0,0] for each. Obviously I am wrong, but I don't know what to do to fix it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…