【发布时间】:2012-04-25 18:24:48
【问题描述】:
我有一个带有 NSSortDesctriptors 的 NSFetchRequest 附加到 NSFetchedResultController 为处于分组模式并显示部分标题的 UITableView 提供数据(参见表示例)。
我在正确排序以显示部分时遇到了一些问题。
数据:
+---+----------+------------+----------+
|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;
}
【问题讨论】:
-
您可以以编程方式添加一个核心数据实体属性,该属性代表您实际需要的排序,但这不能成为更大数据集的解决方案。
标签: objective-c core-data ios5