gpt4 book ai didi

ios - NSFetchedResultsController 与 NSFetchedResultsController UILocalizedIndexedCollat​​ion

转载 作者:技术小花猫 更新时间:2023-10-29 10:16:42 25 4
gpt4 key购买 nike

我正在尝试使用具有混合语言数据的 FRC,并希望有一个部分索引。

从文档看来您应该能够覆盖 FRC

- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName
- (NSArray *)sectionIndexTitles

然后使用 UILocalizedIndexedCollat​​ion 获得本地化索引和部分。但遗憾的是,这不起作用,也不是打算使用的:(

有没有人能够将 FRC 与 UILocalizedIndexedCollat​​ion 一起使用,或者我们是否被迫使用示例 UITableView + UILocalizedIndexedCollat​​ion 示例中提到的手动排序方法(示例代码包含在我开始工作的地方)。

使用以下属性

@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 协议(protocol)来接收更新通知。似乎没有什么好的方法可以让这两个对象很好地协同工作。

最佳答案

由于您无法对 transient 属性进行排序,我实现的解决方案是...

  1. 为核心数据模型中每个实体中的每个可排序属性创建一个名为“sectionKey”的字符串属性。 sectionKey 属性将是从基本属性(例如,名称或标题属性)派生的计算值。它必须持久化,因为(目前) transient 属性不能用于获取请求的排序描述符中。在将提供排序的每个 sectionKey 和基本属性上启用索引。为了将此更新应用到现有应用,您将需要执行轻量级迁移,并且还包括更新预先存在的数据库的例程。

  2. 如果您正在播种数据(例如,用一组标准数据填充新安装,或为每种目标语言创建本地化的 SQLite 数据库,其中一个将在初始启动时被复制),那么编码、计算和更新每个实体的 sectionKey 属性。关于播种数据的“最佳”方法意见不一,但值得注意的是,每种语言的少量 plist 文件(通常范围从几个字节到 20k,即使对于包含数百个值的列表也是如此)与每种语言的单个 SQLite 数据库相比(每种语言的起始大小约为 20k),总体占用空间要小得多。附带说明一下,可以将 Microsoft Excel for Mac 配置为通过启用语言功能 (3) 来提供列表的本地化排序。

  3. 在获取的结果 Controller 构造函数中,对 sectionKey 和 base 属性进行排序,并为部分名称键路径传递 sectionKey。

  4. 添加计算逻辑以更新所有添加或编辑用户输入中的 sectionKey 属性,例如,在 textFieldDidEndEditing: 中。

就是这样!无需手动将获取的对象划分为数组数组。 NSFetchedResultsController 将为您进行本地化整理。例如,在中文(简体)的情况下,获取的对象将按拼音发音(4)进行索引。

(1) 来自 Apple IOS 开发者库 > 国际化编程主题 > Internationalization and Localization .(2) TableViewSuite的3_SimpleIndexedTableView .(3) How to enable Chinese language features in Microsoft Office for Mac .(4) 汉语通常按笔画数或拼音排序。

关于ios - NSFetchedResultsController 与 NSFetchedResultsController UILocalizedIndexedCollat​​ion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7199934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com