ios - 使用 NSFetchedResultsController 在 tableView 中没有数据 View

标签 ios objective-c uitableview core-data

我在我的 tableview 中使用 NSFetchedResultsController,它工作正常。我的问题是,当我的 _fetchedResultsController 有 0 行时,我想创建“无数据 View ”。

我试着在我的 numberOfSectionsInTableView: 中像这样创建它

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([[_fetchedResultController.sections firstObject] numberOfObjects] == 0) {
        MyNoDataView *view = [[MyNoDataView alloc] initWithFrame:tableView.frame];
        tableView.backgroundView = view;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return 0;
    }
    else {
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.backgroundView = nil;
        return  1;  
    }
}

效果很好。如果还没有插入数据,它不会显示数据 View ,如果有数据,它会显示我的表格 View 。

当我对其行进行删除操作时,我的问题就开始了。从技术上讲,我只是更新它的对象属性 (isActive) 作为我在 fetchedResultController 中的 fetchRequest 使用这个 NSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isActive == %@ ", @YES];
[fetchRequest setPredicate:predicate];

然后,如果只有 1 行,我将其删除,则会出现此错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0'

所以我尝试删除我的 numberOfSectionsInTableView:,突然那些错误就消失了。我不知道这里发生了什么,但我认为我在 numberOfSectionsInTableView: 上的条件有误。

如有任何帮助,我们将不胜感激。非常感谢!

最佳答案

已解决

基于 Sneak的答案,分配“MyNoDataView”并在 numberOfSectionsInTableView 中设置 seperatorstyles 是非常糟糕的做法。所以我最终在获取的结果 Controller 委托(delegate)上修改了我的 controllerDidChangeContent:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    if ([controller.fetchedObjects count]) {
        self.tableView.backgroundView = nil;
    }
    else {
        MyDataView *view = [[MyDataView alloc] initWithFrame:self.tableView.frame];
        self.tableView.backgroundView = view;
    }

    [self.tableView endUpdates];
}

关于ios - 使用 NSFetchedResultsController 在 tableView 中没有数据 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331684/

相关文章:

iphone - 使用 UITableView 的 Interface Builder

iphone - dequeueReusableCellWithIdentifier 使用 Storyboard静态单元格返回 nil

iphone - iOS 编程中在不同 View 中加载Webview

ios 8 在滚动时隐藏导航 Controller 并在向上滚动一点时再次显示

ios - ImageView |无法识别的选择器发送到实例 | Xcode 6.4 | iOS 8.4

ios - 动态 UITableView 由同一 ViewController 中的第二个 UITableView 控制 - Swift

jquery - jquery $.get 或 cordova-plugin-media 是否使用或合并加密技术?

ios - 信息灯按钮动画

ios - Swift 3 - Mapbox - 自定义用户位置注释

ios - 可重用的 UICollectionViewCell 不刷新