Uitableview 使用核心数据和NSFetchedResultsController将两组信息合并为标题

Uitableview 使用核心数据和NSFetchedResultsController将两组信息合并为标题,uitableview,core-data,nsfetchedresultscontroller,cells,sections,Uitableview,Core Data,Nsfetchedresultscontroller,Cells,Sections,我有一个简单的应用程序,带有一个加号按钮的表视图;用户单击该按钮并进入模式视图,在该视图中可以输入日期、名称、金额和标题。当他们点击save时,VC将退出,信息保存到数据库中;然后,表视图将更新以显示该信息 因此,用户将名称、标题、日期和金额添加到视图控制器中,并将其转换为表视图,如下所示: CelltextLabel=名称和标题 DetailTextLabel=金额 节头=日期 所以总是有4组信息,但这看起来有点混乱。我正在使用核心数据和NSFetchedResultsController

我有一个简单的应用程序,带有一个加号按钮的表视图;用户单击该按钮并进入模式视图,在该视图中可以输入日期、名称、金额和标题。当他们点击save时,VC将退出,信息保存到数据库中;然后,表视图将更新以显示该信息

因此,用户将名称、标题、日期和金额添加到视图控制器中,并将其转换为表视图,如下所示:

  • CelltextLabel=名称和标题
  • DetailTextLabel=金额
  • 节头=日期
所以总是有4组信息,但这看起来有点混乱。我正在使用核心数据和NSFetchedResultsController。我已经搜索过了,但找不到太多的引用,但是否可以将日期和标题作为章节标题,而不仅仅是日期

如果是,对此有任何帮助将不胜感激

对于构成节标题的关键组件,我的代码如下所示:

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"occasion.dateOfEvent" cacheName:nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id  sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
CellForRowatinex为:

cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", info.name, info.title];
cell.detailTextLabel.text = info.amount;

非常感谢

只需向托管对象子类添加一个属性,如:

- (NSString *)dateAndTitle {
    return [NSString stringWithFormat:@"%@ %@", self.date, self.title];
}
并使用
dataAndTitle
sectionNameKeyPath
初始化您的
NSFetchedResultsController
,如下所示:

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"occasion. dateAndTitle" cacheName:nil];
确保您的获取请求按日期和标题排序,否则您将看到重复的部分