Ios 具有核心数据和非核心数据源的UITableView

Ios 具有核心数据和非核心数据源的UITableView,ios,objective-c,uitableview,core-data,nsfetchedresultscontroller,Ios,Objective C,Uitableview,Core Data,Nsfetchedresultscontroller,我有一个iOS核心数据应用程序,有一些表视图问题。应用程序中有一个主视图控制器,其中包含一个表视图和一个单独的创建者视图控制器,该控制器使用填充的数据创建核心数据管理对象 该应用程序当前的目标是将核心数据对象中的文本加载到表格视图单元格(在MVC表格视图中),当点击该单元格时,在下方(下拉)显示一个新的单元格,显示其他信息 我在-[tableView:cellforrowatinedexpath:中遇到问题,我试图使用[self.fetchedResultsController objectAt

我有一个iOS核心数据应用程序,有一些表视图问题。应用程序中有一个
主视图控制器
,其中包含一个表视图和一个单独的
创建者视图控制器
,该控制器使用填充的数据创建核心数据管理对象

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

我在
-[tableView:cellforrowatinedexpath:
中遇到问题,我试图使用
[self.fetchedResultsController objectAtIndexPath:indexPath]访问特定的核心数据对象
我有一个数组,它保存子单元的所有索引路径,因此在
-[tableView:numberOfRowsInSection:][code>

如果任何人有任何建议,或者如果我需要提供澄清,请让我知道。我不想做一个混乱的代码转储,所以我要展示我认为是
MasterViewController.h
文件的适用部分。(如果我有太多,也请让我知道)


好吧,如果你继续下去,你现在要做的事情很快就会变得非常混乱

相反,我会改变您的方法,使没有子单元格的父单元格显示为当前单元格类型,而有子单元格的父单元格显示为不同的单元格类型,其中包括父单元格和子单元格的内容

这样,您的索引就不会全部混乱。您可以简化节和行计数。(顺便说一句,您不应该运行新的获取来获取计数,而应该从FRC获取计数)


唯一稍微复杂的位是确定行高和要使用的单元格类型…

这样从FRC创建的单元格可以有另一个与其关联的单元格?每个父单元格只能有一个子(或多个子)?在表视图中,每个父单元格只能有一个子单元格。Fetched Results控制器返回用于父单元格的对象,是的。我认为“将变得非常混乱”是一种轻描淡写的说法所以说真的,将会发生的只是父细胞高度的扩展,而不是一个全新的“子细胞”的创建。这就是你所说的吗?是的,没错。试图操纵所有索引路径,以便在添加子元素作为实际单元格时从FRC中获取数据,这将是一个令人讨厌的代码混乱。是的,这正是我的问题,这是一个非常好的解决方法。非常感谢!
@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];

    }