objective-c – 使用NSFetchedResultsController删除节中的最后一行 – >崩溃

objective-c – 使用NSFetchedResultsController删除节中的最后一行 – >崩溃,第1张

概述我正在使用NSFetchedResultsController.以前我有一个类似的问题,当数据库没有tableview的条目,但后来创建一个,我发现必须至少有一个部分,所以我修复了.但是现在它崩溃了,例如我有两个部分,每个部分有一行,我删除了一行,所以部分应该不见了 – >崩溃.它表示更新前的部分数(2)不等于删除的数量(0). - (NSInteger)numberOfSectionsInTab 我正在使用NSFetchedResultsController.以前我有一个类似的问题,当数据库没有tablevIEw的条目,但后来创建一个,我发现必须至少有一个部分,所以我修复了.但是现在它崩溃了,例如我有两个部分,每个部分有一行,我删除了一行,所以部分应该不见了 – >崩溃.它表示更新前的部分数(2)不等于删除的数量(0).

- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{    // Return 1 if the fetchedResultsController section count is zero    return [[fetchedResultsController sections] count] ? : 1;}- (Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section {    // check if we really have any sections in the managed object:    if (!fetchedResultsController.sections.count) return @"Persoonlijk";    ID <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];    return [sectionInfo name];}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{    // check if we really have any sections in the managed object:    if (!fetchedResultsController.sections.count) return 0;    ID <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];    return [sectionInfo numberOfObjects];}

更新
行被删除的方法:

- (voID)tableVIEw:(UItableVIEw *)tableVIEw commitEditingStyle:(UItableVIEwCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    if (editingStyle == UItableVIEwCellEditingStyleDelete) {        // Delete schedule        NSManagedobjectContext *context = [fetchedResultsController managedobjectContext];        [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];        // Save the context.        NSError *error = nil;        if (![context save:&error]) {            NSLog(@"Unresolved error %@,%@",error,[error userInfo]);            exit(-1);        }   } }
解决方法 我找到了问题/解决方案:

对于这种情况,我没有必需的dIDChangeSection委托方法!

- (voID)controller:(NSFetchedResultsController *)controller dIDChangeSection:(ID <NSFetchedResultsSectionInfo>)sectionInfo           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {NSLog(@"dIDChangeSection");    switch(type) {        case NSFetchedResultsChangeInsert:            [self.tableVIEw insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UItableVIEwRowAnimationFade];            break;        case NSFetchedResultsChangeDelete:            [self.tableVIEw deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UItableVIEwRowAnimationFade];            break;    }}
总结

以上是内存溢出为你收集整理的objective-c – 使用NSFetchedResultsController删除节中的最后一行 – >崩溃全部内容,希望文章能够帮你解决objective-c – 使用NSFetchedResultsController删除节中的最后一行 – >崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1031460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存