При выгрузке данных из СoreData в разные UITableViewCell происходите ошибка:
NSFetchedResultsController: no section at index 1 in sections list
CoreData: error: NSFetchedResultsController: no section at index 1 in sections list
2017-12-23 16:18:23.105 tableViewWithDifferentEntities[62887:4185473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'no section at index 1'
Почему пишет, что нет раздела под индексом 1 , если я передаю 2йку в numberOfSections?
Код программы
import UIKit
import CoreData
class TableView: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate{
@IBOutlet weak var myTableView: UITableView!
var fetchedResultsController = CoreDataManager.instance.fetchedResultsController("Customer", keyForSort: "name")
var fetchedResultsController2 = CoreDataManager.instance.fetchedResultsController("Seller", keyForSort: "name_seller")
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate=self
myTableView.dataSource=self
fetchedResultsController.delegate = self
fetchedResultsController2.delegate = self
do {
try fetchedResultsController.performFetch()
} catch {
print(error)
}
do {
try fetchedResultsController2.performFetch()
} catch {
print(error)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
let sections = fetchedResultsController.sections
return sections![0].numberOfObjects
}
else {
let sections1 = fetchedResultsController2.sections
return sections1![0].numberOfObjects
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "0", for: indexPath) as! TableViewCell
let customer = fetchedResultsController.object(at: indexPath) as! Customer
cell.textLabel?.text = customer.name
return (cell)
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "1", for: indexPath) as! TableViewCell2
// ошибка была в этой строке
//let seller = fetchedResultsController2.object(at: indexPath) as! Seller
let seller = fetchedResultsController2.object(at: [0, indexPath.row]) as! Seller
cell2.textLabel?.text = seller.name_seller
return (cell2)
}
}
@IBAction func AddCustomer(_ sender: AnyObject) {
performSegue(withIdentifier: "customersToCustomer", sender: nil)
}
@IBAction func AddSeller(_ sender: AnyObject) {
performSegue(withIdentifier: "sellersToSeller", sender: nil)
}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
myTableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
let section = controller == fetchedResultsController ? 0 : 1
switch type {
case .insert:
myTableView.insertRows(at: [[section, newIndexPath!.row]], with: .automatic)
case .update:
if section == 0 {
let customer = controller.object(at: indexPath!) as! Customer
let cell = myTableView.cellForRow(at: [0, indexPath!.row])
cell!.textLabel?.text = customer.name
} else {
let seller = controller.object(at: indexPath!) as! Seller
let cell2 = myTableView.cellForRow(at: [1, indexPath!.row])
cell2!.textLabel?.text = seller.name_seller
}
case .delete:
myTableView.deleteRows(at: [[section, (indexPath?.row)!]], with: .automatic)
default: break
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
myTableView.endUpdates()
}
}