グループ化モードでセクションヘッダーを表示する UITableView にデータを提供する NSFetchedResultController に NSSortDesctriptors が接続された NSFetchRequest があります (表の例を参照)。
セクションを表示するための適切な並べ替えについて頭を悩ます問題がいくつかあります。
データ:
+---+----------+------------+----------+
|id | subTitle | groupTitle | distance |
+---+----------+------------+----------+
|1 | A | T1 | 1.1 |
+---+----------+------------+----------+
|2 | B | T1 | 1.2 |
+---+----------+------------+----------+
|3 | C | T1 | 3.0 |
+---+----------+------------+----------+
|4 | D | T2 | 1.3 |
+---+----------+------------+----------+
|5 | E | T2 | 1.4 |
+---+----------+------------+----------+
|6 | F | T3 | 1.5 |
+---+----------+------------+----------+
私が持っているもの:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"groupTitle" ascending:YES];
--T1
------A
------B
------C
--T2
------D
------E
--T3
------F
私が欲しいもの:(距離でソートしますが、同じグループ内の次の各アイテムについてgroupTitleでグループ化します)
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
--T1
------A
------B
--T2
------D
------E
--T3
------F
--T1
------C
どうすればこの動作を実現できますか? おそらくviewForHeaderInSectionメソッドで?グループ化するセクションが の最初の階層にない場合はどうなり[self.fetchedResultsController sections]
ますか?
//custom header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
SomeObject *cellEntity = (SomeObject *)[[sectionInfo objects] objectAtIndex:0];
//cellEntity.groupTitle is the title
return aView;
}