ios - 具有核心数据和非核心数据源的 UITableView

标签 ios objective-c uitableview core-data nsfetchedresultscontroller

我的 iOS Core Data 应用程序存在一些 TableView 问题。在应用程序中有一个 Master View Controller,它包含一个 TableView 和一个单独的 Creator View Controller,它使用填充的数据创建 Core Data 托管对象。

该应用程序的当前目标是将核心数据对象中的文本加载到表格 View 单元格(在 MVC 表格 View 中),并且当点击该单元格时,在下方(下拉)显示一个新单元格,显示附加信息信息。

我在 -[tableView:cellForRowAtIndexPath:] 中遇到问题,我尝试使用 [self.fetchedResultsController objectAtIndexPath:indexPath]; 访问特定的核心数据对象> 由于试图实例化额外的子单元格,我无法在正确的索引路径上调用正确的对象。如果我有足够的父细胞,我会收到一个不正确的对象作为返回。如果我没有足够的父单元格,我会因为调用索引范围外的对象而出现 SIGABRT 错误。

我有一个数组,其中包含子单元格的所有索引路径,因此在 -[tableView:numberOfRowsInSection:]

中实现了行计数

如果有人有任何建议,或者我需要澄清,请告诉我。我不想做一个困惑的代码转储,所以我展示了我认为是 MasterViewController.h 文件的适用部分。 (如果我太多了,还请提醒我)

@interface MasterViewController ()
@property (nonatomic, strong) NSMutableArray *subIndexPaths;
@end

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Trinomial" inManagedObjectContext:[self managedObjectContext]];
  [fetchRequest setEntity:entity];
  NSError *error = nil;
  NSInteger count = [_managedObjectContext countForFetchRequest:fetchRequest error:&error];

  if ([self.subIndexPaths count] == 0 || !self.subIndexPaths) {
      return count;
}

  return (count + [self.subIndexPaths count]);

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //Child cell created & returned here
  {
  ...
  }

  //Create a parent cell & setup/return
  UITableViewCell *parentCell = [tableView dequeueReusableCellWithIdentifier:kParentCell];
  Trinomial *currentTri = [self.fetchedResultsController objectAtIndexPath:indexPath];
  parentCell.textLabel.text = [currentTri retrieveTrinomial];
  return parentCell;

}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if ([[[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] isEqualToString:kParentCell]) {
    if ([self.subIndexPaths containsObject:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section]]) {
        //When there is already a child cell present

        [self.subIndexPaths removeObject:newIndexPath];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];

    } else {
      //When there isn't a child cell present

      [self.subIndexPaths addObject:newIndexPath];
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      [self.tableView reloadData];

    }

最佳答案

好的,如果您继续,您当前尝试做的事情很快就会变得非常困惑。

相反,我会更改您的方法,以便任何没有子单元格的父单元格显示为当前单元格类型,而任何有子单元格的父单元格显示为不同的单元格类型 - 其中包括 parent 和 child 的内容

这样,您的索引就不会一团糟。您的节数和行数变得简单。 (顺便说一句,您不应该运行新的提取来获取计数,而是从 FRC 获取它)。

唯一有点复杂的是确定行高和要使用的单元格类型...

关于ios - 具有核心数据和非核心数据源的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734528/

相关文章:

objective-c - 在特定时间运行 Mac 应用程序方法涉及哪些步骤?

ios - 如何在 UITableView 中插入多行

ios - FaSTLane 快照警告 "Unable to mount DeveloperDiskImage"

iphone - 用于 iPhone/iPad 的 HTML 单位

ios - 在多个 OpenGL-ES 上下文之间共享一个 glProgram?

ios - UITableViewCell的CAGradient层问题

ios - 使用自动布局约束动态调整表格 View 单元格的大小

ios - 预填充 UICollectionView 单元重用队列

objective-c - 使用哪种数据结构来传递值对?

iphone - 如何减小 objective-c 中图像的大小