核心数据中各部分索引的整个字母表

发布于 2024-12-23 15:37:59 字数 1290 浏览 1 评论 0原文

我已经实现了一个填充了核心数据的表,现在我尝试通过显示侧面字母(以类似联系人的格式)来用部分对其进行索引。

在下面的代码中,如果我使用注释行,则只有现有部分的字母。但我想要整个字母表,因此我更改了返回的数组:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{    
    //return [fetchedResultsController sectionIndexTitles];    

    indexArray = [NSArray arrayWithObjects: @"{search}", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J",@"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"#", nil];
    return indexArray;
}

所有字母都显示在侧面索引上。但现在我必须实现返回所选部分索引的方法,这里我遇到了一些问题:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];

    NSString *correspondingLetter = [indexArray objectAtIndex:index];
    NSUInteger correspondingIndex = [[fetchedResultsController sections] indexOfObject:correspondingLetter];

    NSLog(@"------index:%i\ncorrespondingLetter: %@\ncorrespondingIndex: %i\n", index,correspondingLetter, correspondingIndex);

    return correspondingIndex;
}

使用上面的代码,如果我使用注释行,每次选择一个不包含注释的字母时都会出现错误没有对应的版块。所以我想做的是使用已选择的字母检索部分索引并将其位置搜索到现有部分中。但这不起作用。 你有什么想法吗?

提前致谢, 亚萨

I've implemented a table populated with core data and now I'm trying to indexing it with sections by displaying the side Alphabet (in Contacts like format).

In the code below, if I use the commented line, I have only letters for existing sections. But I want the whole alphabet, and so I've changed the returned array:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{    
    //return [fetchedResultsController sectionIndexTitles];    

    indexArray = [NSArray arrayWithObjects: @"{search}", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J",@"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"#", nil];
    return indexArray;
}

All the letters are displayed on the side index. But now I've to implement the method that returns the index of the selected section, and here I've some problems:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];

    NSString *correspondingLetter = [indexArray objectAtIndex:index];
    NSUInteger correspondingIndex = [[fetchedResultsController sections] indexOfObject:correspondingLetter];

    NSLog(@"------index:%i\ncorrespondingLetter: %@\ncorrespondingIndex: %i\n", index,correspondingLetter, correspondingIndex);

    return correspondingIndex;
}

With the code above, if I use the commented line, I have an error each time I select a letter that doesn't have the corresponding section. So what I'm trying to do, is to retrieve the section index using the letter that has been selected and searching its position into existing sections. But it doesn't work.
Do you have any ideas?

Thanks in advance,
yassa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷夜 2024-12-30 15:37:59

您应该在

@property (nonatomic, readonly) NSArray *sectionIndexTitles

fetchedResultController 中进行搜索。
但是,如果它是不存在的索引,您将收到 NSNotFound 并且需要执行一些逻辑来返回前一个字母索引,或前一个字母索引,或前一个字母索引等。

You should search in

@property (nonatomic, readonly) NSArray *sectionIndexTitles

of the fetchedResultController.
But, if it's an index that doesn't exist you will receive NSNotFound and will need to do some logic to return the previous letter index, or the previous, or the previous, etc.

雾里花 2024-12-30 15:37:59

为了支持其他语言,我不会使用硬编码字符。我的解决方案受到公认答案的启发:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSString *correspondingLetter = [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] objectAtIndex:index];
    return [[self.fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];
}

To support additional languages I wouldn't use hard coded characters. My solution inspired by the accepted answer :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSString *correspondingLetter = [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] objectAtIndex:index];
    return [[self.fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文