避免使用 NSSortDescriptor 进行排序

发布于 2024-10-19 06:38:42 字数 1636 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-10-26 06:38:42

我假设您将核心数据与 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文