A-Z从NSFetchedResultsController索引,每个字母内有单独的节头? [英] A-Z Index from NSFetchedResultsController with individual section headers within each letter?

查看:172
本文介绍了A-Z从NSFetchedResultsController索引,每个字母内有单独的节头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSFetchedResultsController ,它从Core Data结构(一个相册列表)中获取数据。它目前是按照艺术家排序的,所以所有的A,B的,等等。我想添加一个索引,以便用户可以快速跳转到每个字母,我使用下面的代码来做到这一点。问题是,部分标题现在是A,B,C等,这意味着我失去了部分标题按字母顺序与每个艺术家对它(阿黛尔,美国,Beatles,The等)



我想索引使用字母A到Z,但是部分标题显示艺术家姓名按字母顺序。在索引中推一个字母,然后跳转到第一个艺术家与那封信。如何实现这一点(索引中的字母字符,但部分标题的不同属性)?



编辑 sectionNameKeyPath 到我的作者字段,AZ索引是可见的,但字母不会同步到名称,例如,如果我点击S,它需要我到以AL开头的艺术家,或点击G带我到AB。我不太清楚为什么会是这样,也许段索引正在旋转?



这里是我使用的代码:



在我的 NSManagedObject 子类中:

 code>  - (NSString *)firstLetter 
{
[self willAccessValueForKey:@firstLetter];
NSString * firstLetter = [[[self author] substringToIndex:1] uppercaseString];
[self didAccessValueForKey:@firstLetter];
return firstLetter;
}

在我的View Controller中,创建FRC:

  NSFetchedResultsController * byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest:_fetchRequest 
managedObjectContext:[[CDManager sharedManager] managedObjectContext]
sectionNameKeyPath:@firstLetter
cacheName:nil];
sectionIndexTitlesForTableView:方法:

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


解决方案

sectionNameKeyPath:@author是正确的参数,因为这是
如何让你的表格分组。标准实现

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

返回每个节头的第一个字母,因此不需要单独
firstLetter 方法。



对于从节索引到
的正确同步节
必须实现表视图数据源方法 tableView:sectionForSectionIndexTitle:atIndex:。使用获取的结果控制器的
标准实现是:

   - (NSInteger)tableView:(UITableView *) tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index]
}

编辑



Swift:

  func sectionIndexTitlesForTableView(tableView:UITableView) - > [串]? {
return fetchedResultsController.sectionIndexTitles
}

func tableView(tableView:UITableView,sectionForSectionIndexTitle title:String,atIndex index:Int) - > Int {
return fetchedResultsController.sectionForSectionIndexTitle(title,atIndex:index)
}


I have an NSFetchedResultsController which fetches data from a Core Data structure, a list of albums. It's currently sorted by the artist, so all the A's, B's, etc. I want to add an index so the user can quickly jump to each letter, and I'm using the code below to do it. The issue is that the section headers are now "A", "B", "C", etc., too, meaning I've lost the section headers in alphabetical order with each individual artist on it ("Adele", "America", "Beatles, The", etc.)

I'd like the index to use the letters A through Z, but the section headers to display the artist names in alphabetical order. Pushing a letter in the index would then jump to the first artist with that letter. How can I achieve this (alphabet characters in the index, but a different attribute for the section titles)?

EDIT: If I set my sectionNameKeyPath to my author field, the A-Z index is visible, but the letters aren't synced up to the names, for example if I tap S, it takes me to artists beginning with AL, or tapping G takes me to AB. I'm not quite sure why this is, perhaps the section index is screwing up?

Here's the code I'm using:

In my NSManagedObject subclass:

- (NSString *) firstLetter
{
    [self willAccessValueForKey: @"firstLetter"];
    NSString *firstLetter = [[[self author] substringToIndex: 1] uppercaseString];
    [self didAccessValueForKey: @"firstLetter"];
    return firstLetter;
}

In my View Controller, to create the FRC:

NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest
                                                                              managedObjectContext: [[CDManager sharedManager] managedObjectContext]
                                                                                sectionNameKeyPath: @"firstLetter"
                                                                                         cacheName: nil];

And here's my sectionIndexTitlesForTableView: method:

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

解决方案

sectionNameKeyPath:@"author" is the correct parameter, because that is how you want your table grouped into sections. The standard implementation

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

returns the first letter of each section header, so there is no need for a separate firstLetter method.

For the correct synchronization from the section index to the sections you have to implement the table view data source method tableView:sectionForSectionIndexTitle:atIndex:. The standard implementation in connection with a fetched results controller is:

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

Edit:

Swift:

func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    return fetchedResultsController.sectionIndexTitles
}

func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
    return fetchedResultsController.sectionForSectionIndexTitle(title, atIndex: index)
} 

这篇关于A-Z从NSFetchedResultsController索引,每个字母内有单独的节头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆