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

yas*_*sin 5 indexing core-data alphabet ios

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢,yassa

Vin*_*ier 5

你应该搜索一下

@property (nonatomic, readonly) NSArray *sectionIndexTitles
Run Code Online (Sandbox Code Playgroud)

fetchedResultController.
但是,如果它是一个不存在的索引,你将收到NSNotFound并将需要做一些逻辑来返回前一个字母索引,或前一个,或前一个,等等.