我正在尝试将 FRC 与混合语言数据一起使用,并希望有一个部分索引。
从文档看来,您应该能够覆盖 FRC
- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName
- (NSArray *)sectionIndexTitles
然后使用 UILocalizedIndexedCollation 获得本地化索引和部分。但遗憾的是这不起作用,也不是预期用途:(
有没有人能够将 FRC 与 UILocalizedIndexedCollation 一起使用,或者我们被迫使用示例 UITableView + UILocalizedIndexedCollation 示例中提到的手动排序方法(示例代码包含在我工作的地方)。
使用以下属性
@property (nonatomic, assign) UILocalizedIndexedCollation *collation;
@property (nonatomic, assign) NSMutableArray *collatedSections;
和代码:
- (UILocalizedIndexedCollation *)collation
{
if(collation == nil)
{
collation = [UILocalizedIndexedCollation currentCollation];
}
return collation;
}
- (NSArray *)collatedSections
{
if(_collatedSections == nil)
{
int sectionTitlesCount = [[self.collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
collatedSections = newSectionsArray;
NSMutableArray *sectionsCArray[sectionTitlesCount];
// Set up the sections array: elements are mutable arrays that will contain the time zones for that section.
for(int index = 0; index < sectionTitlesCount; index++)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
sectionsCArray[index] = array;
[array release];
}
for(NSManagedObject *call in self.fetchedResultsController.fetchedObjects)
{
int section = [collation sectionForObject:call collationStringSelector:NSSelectorFromString(name)];
[sectionsCArray[section] addObject:call];
}
NSArray *sortDescriptors = self.fetchedResultsController.fetchRequest.sortDescriptors;
for(int index = 0; index < sectionTitlesCount; index++)
{
[newSectionsArray replaceObjectAtIndex:index withObject:[sectionsCArray[index] sortedArrayUsingDescriptors:sortDescriptors]];
}
}
return [[collatedSections retain] autorelease];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// The number of sections is the same as the number of titles in the collation.
return [[self.collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// The number of time zones in the section is the count of the array associated with the section in the sections array.
return [[self.collatedSections objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if([[self.collatedSections objectAtIndex:section] count])
return [[self.collation sectionTitles] objectAtIndex:section];
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
我希望仍然能够使用 FRCDelegate 协议来获取更新通知。似乎没有什么好的方法可以让这两个对象很好地协同工作。
由于您无法对瞬态属性进行排序,因此我实施的解决方案是......
为核心数据模型中每个实体内的每个可排序属性创建一个名为“sectionKey”的字符串属性。 sectionKey 属性将是从基本属性(例如名称或标题属性)派生的计算值。它必须被持久化,因为(当前)瞬态属性不能在获取请求的排序描述符中使用。对将提供排序的每个sectionKey 和基本属性启用索引。为了将此更新应用于现有应用程序,您将需要执行轻量级迁移,并且还包括更新预先存在的数据库的例程。
如果您正在播种数据(例如,用一组标准数据填充新安装,或者为每种目标语言创建本地化 SQLite 数据库,其中一个数据库将在首次启动时复制),请在该代码中计算并更新每个目标语言实体的sectionKey 属性。对于播种数据的“最佳”方法有不同的看法,但值得注意的是,每种语言的少数 plist 文件(通常范围从几个字节到 20k,即使是由数百个值组成的列表)也会留下比每种语言的单个 SQLite 数据库(每种语言起价约为 20k)的总体占用空间要小得多。另外,可以将 Microsoft Excel for Mac 配置为通过启用语言功能 (3) 来提供列表的本地化排序。
在获取的结果控制器构造函数中,对sectionKey 和base 属性进行排序,并传递sectionKey 作为节名称键路径。
添加计算逻辑以更新所有添加或编辑用户输入中的sectionKey属性,例如,在textFieldDidEndEditing:中。
就是这样!无需手动将获取的对象划分为数组的数组。 NSFetchedResultsController 将为您进行本地化排序。例如,对于中文(简体),获取的对象将按语音发音进行索引(4)。
(1) 来自 Apple IOS 开发者库 > 国际化编程主题 >国际化和本地化 https://developer.apple.com/library/ios/#documentation/MacOSX/Conceptual/BPInternational/Articles/InternatAndLocaliz.html。
(2) 3_SimpleIndexedTableView的表视图套件 http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html.
(3) 如何在 Microsoft Office for Mac 中启用中文功能 http://mac2.microsoft.com/help/office/14/en-us/word/item/c53ed219-462d-485a-b4f3-c3bd906874a1。
(4) 汉语通常按笔画或拼音排序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)