Ios 使用CoreData自定义SectionHeaderView

Ios 使用CoreData自定义SectionHeaderView,ios,swift,uitableview,core-data,Ios,Swift,Uitableview,Core Data,我的自定义分区HeaderView有问题。我得到以下错误: 错误:严重的应用程序错误。从中捕获了一个异常 调用时NSFetchedResultsController的委托 -controllerDidChangeContent: 无效更新:节数无效。之后表视图中包含的节数 更新(2)必须等于中包含的节数 更新前的表视图(2),加上或减去 插入或删除节(插入1节,删除0节)。使用userInfo (空) 如果我使用正常的标题作为标题部分,一切正常 更改为viewForHeaderInSection

我的自定义分区HeaderView有问题。我得到以下错误:

错误:严重的应用程序错误。从中捕获了一个异常 调用时NSFetchedResultsController的委托
-controllerDidChangeContent:

无效更新:节数无效。之后表视图中包含的节数 更新(2)必须等于中包含的节数 更新前的表视图(2),加上或减去 插入或删除节(插入1节,删除0节)。使用userInfo (空)

如果我使用正常的
标题作为标题部分
,一切正常

更改为
viewForHeaderInSection
会导致错误。这是ViewForHeaderIn部分的代码:

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
}
这是我的
获取结果控制器

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;
    }
}
func控制器(controller:NSFetchedResultsController,didChange anObject:Any,在indexPath:indexPath?处,对于类型:NSFetchedResultsChangeType,newIndexPath:indexPath?){
开关类型{
案例.插入:
guard let newindepath=newindepath else{return}
tableView.insertRows(位于:[newIndexPath],带:。自动)
案例.删除:
guard let indepath=indepath else{return}
tableView.deleteRows(位于:[indexPath],带:。自动)
案例。更新:
guard let indepath=indepath else{return}
tableView.reloadRows(位于:[indexPath],带:。自动)
案件.动议:
guard let indepath=indepath,let newindepath=newindepath else{return}
如果indexPath==newIndexPath{
tableView.reloadData()
}否则{
moveRow(at:indepath,to:newindepath)
}
}
}
func控制器(控制器:NSFetchedResultsController,didChange sectionInfo:NSFetchedResultsSectionInfo,atSectionIndex sectionIndex:Int,类型:NSFetchedResultsChangeType){
开关类型{
案例.插入:
tableView.insertSections(IndexSet(整数:sectionIndex),带:.fade)
案例.删除:
tableView.deleteSections(IndexSet(整数:sectionIndex),带:.fade)
违约:
打破
}
}

感谢您的帮助

您是否使用tableView.BeginUpdate()和tableView.EndUpdate()实现了/?是