核心数据中各部分索引的整个字母表
我已经实现了一个填充了核心数据的表,现在我尝试通过显示侧面字母(以类似联系人的格式)来用部分对其进行索引。
在下面的代码中,如果我使用注释行,则只有现有部分的字母。但我想要整个字母表,因此我更改了返回的数组:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在
fetchedResultController 中进行搜索。
但是,如果它是不存在的索引,您将收到
NSNotFound
并且需要执行一些逻辑来返回前一个字母索引,或前一个字母索引,或前一个字母索引等。You should search in
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.为了支持其他语言,我不会使用硬编码字符。我的解决方案受到公认答案的启发:
To support additional languages I wouldn't use hard coded characters. My solution inspired by the accepted answer :