iPhone - 使用NSFetchResultsController将Core Data分成几个部分 [英] iPhone -- breaking up Core Data into sections with NSFetchResultsController

查看:156
本文介绍了iPhone - 使用NSFetchResultsController将Core Data分成几个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经成功实现了Core Data从服务器检索对象,保存它们,并在UITableView中显示它们。然而,现在,我想将这些分成几个部分。我已经找了几天了,NSFetchedResultsController似乎混淆我,即使我使用它的方式工作。我在我的实体中有一个称为articleSection的项,当项目添加到核心数据时,如顶部体育生活项目。我将如何打破这些在我的UITableView中的独立部分?我已经阅读关于使用多个NSFetchedResultsControllers,但我大概是如此沮丧,因为这。

So I have successfully implemented Core Data to retrieve objects from a server, save them, and display them in a UITableView. However now, I wish to break these up into separate sections. I have looked for a few days now, and NSFetchedResultsController seems to confuse me, even though the way I am using it works. I have a key in my Entity called "articleSection" that is set when the item is added to Core Data with items such as "Top" "Sports" "Life". How would I go about breaking these into separate sections in my UITableView? I have read about using multiple NSFetchedResultsControllers, but I am about as frustrated as can be with this.

任何建议或帮助将非常感谢。

Any suggestions or help would be greatly appreciated.

推荐答案

NSFetchedResultsController 的文档具有完美工作的示例代码。

The documentation for NSFetchedResultsController has sample code that works perfectly.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = /* get the cell */;
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    // Configure the cell with data from the managed object.
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

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

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






设置sortDescriptors fetch请求,所以结果按articleSection排序。

将sectionKeyPath设置为articleSection,以便NSFetchedResultsController为您创建节。类似这样:


Set the sortDescriptors of the fetch request so the results are sorted by articleSection.
Set the sectionKeyPath to "articleSection" so the NSFetchedResultsController creates the sections for you. Something like this:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];;
request.fetchBatchSize = 20;
// sort by "articleSection"
NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"articleSection" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil];;

// create nsfrc with "articleSection" as sectionNameKeyPath
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"articleSection" cacheName:@"MyFRCCache"];
frc.delegate = self;
NSError *error = nil;
if (![frc performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
self.fetchedResultsController = frc;

这篇关于iPhone - 使用NSFetchResultsController将Core Data分成几个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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