【问题标题】:Whole alphabet for sections index in core data核心数据中部分索引的整个字母表
【发布时间】:2012-01-30 17:46:47
【问题描述】:

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

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

- (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;
}

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

提前致谢, 亚萨

【问题讨论】:

    标签: ios core-data indexing alphabet


    【解决方案1】:

    你应该搜索

    @property (nonatomic, readonly) NSArray *sectionIndexTitles
    

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

    【讨论】:

    • 难以置信,它就在我面前,我没看到!! :) 正如您所建议的,执行以下操作就足够了:correspondingIndex = [[fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];。非常感谢!!!
    【解决方案2】:

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

    - (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];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      相关资源
      最近更新 更多