本文介绍了NSFetchedResultsController section使用显示顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个选项,允许用户更改我的iPhone应用程序中类别的显示顺序。



我想使用NSFetchedResultsController因此段标题是由category.displayOrder排序的category.name,其中category与我正在读取的实体具有TO-ONE关系。我可以使切片正常工作的唯一方法是使用category.displayOrder作为节标题。

  NSSortDescriptor * sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@category.displayOrderascending:YES]; 
NSSortDescriptor * sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@nameascending:YES];

NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2,nil]

[fetchRequest setSortDescriptors:sortDescriptors];

[fetchRequest setFetchBatchSize:10];

NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@category.name
cacheName:nil];

关于如何命名段标题与我排序的属性不同的任何想法? / p>

解决方案

不知道我是否完全理解你的问题,但我在我的应用程序中做类似的事情,这里是如何让它工作:



首先,fetchedResultsController方法,其中我根据我想做什么设置排序描述和谓词。在这种情况下,我想按发布日期THEN按名称排序电影标题。



在我的fetchresultscontroller定义中,将sectionNameKeyPath设置为releaseDate因此我的部分标题将基于日期。

   - (NSFetchedResultsController *)fetchedResultsController {

if(fetchedResultsController_!= nil){
return fetchedResultsController_;
}
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSSortDescriptor * sortByReleaseDate = [[NSSortDescriptor alloc] initWithKey:@releaseDateascending:NO];
NSSortDescriptor * sortByName = [[NSSortDescriptor alloc] initWithKey:@titleascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortByReleaseDate,sortByName,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortByName release];
[sortByReleaseDate release];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@(type =='Movies')AND(releaseDate< =%@)AND(releaseDate> =%@),[NSDate date],[NSDate dateWithTimeIntervalSinceNow :kOneDayTimeInterval * -30]];
[fetchRequest setPredicate:predicate];

NSEntityDescription * entity = [NSEntityDescription entityForName:@MovieinManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@releaseDatecacheName:nil];

... //在这里执行并返回fetch,错误处理等...

return fetchedResultsController_;

}

然后在我的表视图数据源委托方法中,我返回在将我的NSDate转换为NSString之后,每个头的实际标题(记住你必须返回NSString为一个tableview头标题。

  (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

NSString * rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
/ / convert default date string to NSDate ...
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss ZZ ];
NSDate * date = [formatter dateFromString:rawDateStr];

//将NSDate转换为我们想要的格式...
[formatter setDateFormat:@d MMMM yyyy ];
NSString * formattedDateStr = [formatter stringFromDate:date];
return formattedDateStr;

}

因此,如果我想改变我的数据显示方式,例如通过titleName来组织,我会将我的fetchedResultsController对象更改为:

  NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@titleNamecacheName:nil]; 

并修改我的tableview:titleForHeaderInSection:数据源方法只是返回titleName ):

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}



我希望这可以帮助您找到解决您的具体问题的解决方案。 >

干杯,
Rog


I currently have an option that allows a user to change the display order of a category in my iPhone app.

I want to section the table view using a NSFetchedResultsController so that the section titles are the "category.name" ordered by "category.displayOrder" where "category" has a TO-ONE relationship with the entity I am fetching. The only way I can get the sectioning to work correctly is by using "category.displayOrder" as the section title.

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"category.displayOrder" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

[fetchRequest setFetchBatchSize:10];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                                    managedObjectContext:managedObjectContext
                                                                                                      sectionNameKeyPath:@"category.name"
                                                                                                               cacheName:nil];

Any ideas on how I can name the section title something different then the property I am sorting with?

解决方案

Not sure if I understand your question completely, but I do something similar in my app and here's how I get it to work:

Firstly, the fetchedResultsController method, where I set the sort descriptions and predicates based on what I am trying to do. In this case I want to sort movie titles by release date THEN by name. Then with my predicate I grab entities of a specific 'type' and within a certain 'releaseDate' range.

In my fetchresultscontroller definition, you set the sectionNameKeyPath to "releaseDate" so my section headers will be based on a date.

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSSortDescriptor *sortByReleaseDate = [[NSSortDescriptor alloc] initWithKey:@"releaseDate" ascending:NO];
    NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByReleaseDate,sortByName, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortByName release];
    [sortByReleaseDate release];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(type == 'Movies') AND (releaseDate <= %@) AND (releaseDate >= %@)", [NSDate date], [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*-30]];
    [fetchRequest setPredicate:predicate];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movie" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"releaseDate" cacheName:nil];

    ...// Perform and return fetch here, error handling etc...

    return fetchedResultsController_;

}

Then in my table view data source delegate methods, I return the actual title for each header after transforming my NSDate into NSString (remember you have to return NSString for a tableview header title.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
    //convert default date string to NSDate...
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZ"];
    NSDate *date = [formatter dateFromString:rawDateStr];

    //convert NSDate to format we want...
    [formatter setDateFormat:@"d MMMM yyyy"];
    NSString *formattedDateStr = [formatter stringFromDate:date];
    return formattedDateStr;

}

So if I wanted to change the way my data is being displayed to be organised by titleName for instance, I'd change my fetchedResultsController object to:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"titleName" cacheName:nil];

And modify my tableview:titleForHeaderInSection: data source method to simply return the titleName (which is already a string):

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

I hope this helps you find a solution to your specific problem.

Cheers,Rog

这篇关于NSFetchedResultsController section使用显示顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:18