问题描述
我使用NSSortDescriptors来排序核心数据对象,并根据日期属性创建表视图节。使用这种SortDescriptor,表视图部分按预期排序,但部分中的行也按相同的日期属性排序。有没有办法在每个部分内有另一个订购系统?我想主要的问题是核心数据存储日期对象与日期+时间值,那种方式没有完全相同的日期值的对象。但我需要基于另一个属性在同一节内订购对象。
谢谢。
这是NSFetchedResultsController的代码:
code> - (NSFetchedResultsController *)fetchedResultsController {
if(_fetchedResultsController!= nil){
return _fetchedResultsController;
}
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext * context = self.managedObjectContext;
NSEntityDescription * entity = [NSEntityDescription entityForName:@MyEntityinManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@itemDateascending:YES];
NSSortDescriptor * sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@itemNameascending:NO];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor1,nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@sectionIdentifiercacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
已翻译问题
这是添加到cellForRowAtIndexPath方法的代码:
int indexpathsection = indexPath.section;
[self sortedSectionForIndex:indexpathsection];
NSLog(@INDEXPATH =%ld,(long)indexPath.section);
这是y Marcus提出的方法:
- (NSArray *)sortedSectionForIndex:(NSInteger)index
{
NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@priorityascending:YES];
id section = [[self fetchedResultsController] sections] [index];
NSLog(@INDEX ********* =%ld,(long)index);
NSArray * objects = [section objects];
NSArray * sorted = [objects sortedArrayUsingDescriptors:@ [sort]];
return sorted;
}
NSFetchedResultsController
是用于通过单个排序或排序控制的排序顺序。
但你可以这样做,只是不干净。
首先,讨论排序一节。这不需要你自己写排序算法:
- (NSArray *)sortedSectionForIndex:(NSInteger)index
{
NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@MyKeyascending:YES];
id section = [[self fetchedResultsController] sections] [index];
NSArray * objects = [section objects];
NSArray * sorted = [objects sortedArrayUsingDescriptors:@ [sort]];
return sort
}
使用该方法,每当你想要检索一个对象时进行排序。但是这是低效的,因为你每次你想触摸一个对象,在 UITableViewController
是一个 LOT 。取而代之,以这个例子,并与你的 NSFetchedResultsController
和它的委托方法集成,以便你存储排序的数组,并保持它们同步。
更新
我提供给您的排序代码不会对 NSFetchedResultsController
中的内部进行排序。它需要什么是在部分,排序它,并返回您排序的数组。所以你需要使用它返回的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
{
NSArray * sorted = [self sortedSectionForIndex:[indexPath section]];
id object = [sorted objectAtIndex:[indexPath row]];
id cell = ...; //现在得到你的单元格引用
//现在用对象的数据更新你的单元
return cell;
}
I am using NSSortDescriptors to order core data objects and create table view sections depending on a date attribute. Using this kind of SortDescriptor, the table view sections are ordered as expected, but the rows inside the sections are also ordered by the same date attribute. Is there a way to have another ordering system inside each section? I guess the main problem is that core data stores date objects with date+time values, that way there are no objects with exactly the same date value. But I would need to order the objects inside the same section based on another attribute.Thank you.
And here is my code for the NSFetchedResultsController:
-(NSFetchedResultsController*)fetchedResultsController{
if (_fetchedResultsController != nil){
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = self.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"itemDate" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"itemName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor,sortDescriptor1, nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionIdentifier" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
EDITED QUESTION
This is the piece of code added to the cellForRowAtIndexPath method:
int indexpathsection = indexPath.section;
[self sortedSectionForIndex:indexpathsection];
NSLog(@"INDEXPATH= %ld", (long)indexPath.section);
And this is the method proposed y Marcus:
- (NSArray*)sortedSectionForIndex:(NSInteger)index
{
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"priority" ascending:YES];
id section = [[self fetchedResultsController] sections][index];
NSLog(@"INDEX********* = %ld", (long)index);
NSArray *objects = [section objects];
NSArray *sorted = [objects sortedArrayUsingDescriptors:@[sort]];
return sorted;
}
The NSFetchedResultsController
is meant for the sort order to be controlled via a single sort or set of sorts. It is not intended to do what you are trying to do.
But you can do it, it is just not as clean.
First, the discussion of sorting a section. This does not require you to write your own sorting algorithm:
- (NSArray*)sortedSectionForIndex:(NSInteger)index
{
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"MyKey" ascending:YES];
id section = [[self fetchedResultsController] sections][index];
NSArray *objects = [section objects];
NSArray *sorted = [objects sortedArrayUsingDescriptors:@[sort]];
return sorted
}
With that method, you can ask for a section to be sorted whenever you want to retrieve an object. However that is inefficient as you are sorting every time you want to touch an object which in a UITableViewController
is a LOT.
Instead, take this example and integrate it with your NSFetchedResultsController
and its delegate methods so that you are storing the sorted arrays and keeping them in sync. That way you are only doing the sort when the data changes instead of on each method call.
Update
The sort code I provided to you does not sort what is inside of the NSFetchedResultsController
. It takes what is in the section, sorts it and returns you the sorted array. So you need to use what it returns:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *sorted = [self sortedSectionForIndex:[indexPath section]];
id object = [sorted objectAtIndex:[indexPath row]];
id cell = ...; //Now get your cell reference
//Now update your cell with the data from object
return cell;
}
这篇关于在表视图部分中对核心数据对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!