0

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

因此,用户将姓名、标题、日期和金额添加到视图控制器中,然后将其转换为表格视图:

  • CelltextLabel = 名称和标题
  • DetailTextLabel = 金额
  • 部分标题 = 日期

所以总是有 4 组信息,但这看起来有点乱。我正在使用核心数据和 NSFetchedResultsController。我已经搜索并找不到太多对此的引用,但是是否可以将 DATE 和 TITLE 作为节标题而不仅仅是日期?

如果是这样,任何有关这方面的帮助将不胜感激。

对于组成部分标题的关键组件,我的代码如下:

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];
}

CellforRowAtIndex 为:

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

非常感谢

4

1 回答 1

0

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

- (NSString *)dateAndTitle {
    return [NSString stringWithFormat:@"%@ %@", self.date, self.title];
}

并初始化您的NSFetchedResultsController使用dataAndTitlesectionNameKeyPath例如:

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

确保您的提取请求按日期和标题排序,否则您会看到重复的部分。

于 2013-11-11T19:33:08.693 回答