我的自定义sectionHeaderView出现问题。我收到以下错误:
错误:严重的应用程序错误。从中捕获了一个例外 致电期间
NSFetchedResultsController
的代表-controllerDidChangeContent:
。无效更新:无效的部分数量。表视图中包含的节数 update(2)必须等于包含的节数 更新前的表视图(2),加号或减号 插入或删除的部分(插入1个,删除0个)。用户信息 (空)
如果我使用普通titleForHeaderInSection
,一切正常。
更改为viewForHeaderInSection
会导致错误。这是viewForHeaderinSection的代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let currSection = fetchedResultsController.sections?[section] else { return nil }
let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: ContractListHeaderView.reuseIdentifier) as! ContractListHeaderView
print(currSection.name)
headerView.sectionLabel.text = currSection.name
return headerView
}
那是我的fetchedResultsController
:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
guard let newIndexPath = newIndexPath else { return }
tableView.insertRows(at: [newIndexPath], with: .automatic)
case .delete:
guard let indexPath = indexPath else { return }
tableView.deleteRows(at: [indexPath], with: .automatic)
case .update:
guard let indexPath = indexPath else { return }
tableView.reloadRows(at: [indexPath], with: .automatic)
case .move:
guard let indexPath = indexPath, let newIndexPath = newIndexPath else { return }
if indexPath == newIndexPath {
tableView.reloadData()
} else {
tableView.moveRow(at: indexPath, to: newIndexPath)
}
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
break;
}
}
感谢您的帮助