避免使用 NSSortDescriptor 进行排序
我使用 sqlite 数据库详细信息在表视图中显示。使用 NSSortDescriptor 我能够以升序或降序显示表行内容。请帮助我避免排序过程并在输入内容时在表视图中显示内容。 这是我正在使用的代码。它通过对数据进行排序效果很好,帮助我避免排序。 -
(NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"book" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSLog(@"inside table view %@",aFetchedResultsController);
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
Iam using sqlite database details to display in a tableview.Using NSSortDescriptor Iam able to display the table row contents in ascending or descending order.Please help me to avoid the sorting process and display contents in tableview as they are entered.
Here is the code that I was using.It works pretty well by sorting the data,help me to avoid sorting.
-
(NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"book" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSLog(@"inside table view %@",aFetchedResultsController);
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您将核心数据与 sql lite 一起使用。如果是这样,您应该使用 NSFetchedResultsController,网络上有很多如何使用它的示例。
NSFetchedResultsController 需要一个排序描述符,如果您希望值不按顺序排列,请记住您正在基于对控制器获取结果中的元素的 indexPath 引用来填充表视图(-objectAtIndexPath:)。
因此,您可以返回随机索引路径,其行数低于行数
I am assuming you use core data with sql lite. If so you should use a NSFetchedResultsController, there are many examples of how to use that on the web.
NSFetchedResultsController requires a sort descriptor, if you want your values out of order remember that you are populating your table view based on an indexPath reference to an element in your controller's fetched results (-objectAtIndexPath:).
So you can have that give back random indexpaths with rows inferior to row count