ios - 显示包含核心数据的部分

标签 ios xcode core-data

我想编写一个待办事项应用程序并显示 2 个部分。 1 表示必须完成的条目,1 表示已经完成的事情。 我想要这样的东西:

    Things not done yet
    Todo 1
    Todo 2
    Things already done
    Todo 3
    Todo 4

目前我有 1 个部分用于未完成的待办事项。 我目前的 TableView 代码如下所示

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *array = [[NSArray alloc] initWithObjects:@"To do",@"done",nil];

    return [array objectAtIndex:section];
}

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

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

我的 fetchedresultscontroller 代码如下所示:

    - (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSManagedObjectContext* context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription* entity = [NSEntityDescription entityForName:@"Todo" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];


    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:YES];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"done == %@", @(NO)];

    [fetchRequest setPredicate:predicate];


    // 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:self.managedObjectContext sectionNameKeyPath:@"done" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
    }  

键为“NO”的对象未完成,键为“YES”的对象已完成。但是我不知道如何在不同的部分显示这两个值。


编辑

我找到了解决问题的方法……

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

解决了这个问题。但现在有时未完成的项目会在完成时出现。 或者,如果有两个已完成的项目,它们会出现在“待办事项”下

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *array = [[NSArray alloc] initWithObjects:@"Todo",@"Done",nil];

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

这就是我的实际代码…… 现在看起来是这样的。未检查未完成的待办事项,已检查已完成的待办事项。 http://img.xnmn.de/i/2353ad.png 但是,如果我将未完成的待办事项检查为已完成,它们都会出现在“待办事项”http://img.xnmn.de/i/5edff4.png


最佳答案

您的代码看起来几乎是正确的。由于您将 sectionKeyPath 设置为“done”,因此 fetched results Controller 将自动为您创建这些部分。您可以为“完成”属性添加另一个排序描述符,以根据条目的部分对条目进行排序。

必须做的是删除NSPredicate。在您当前的代码中,您使用“完成 == YES”过滤掉所有条目,但由于您有两个部分,您希望结果 Controller 返回所有条目。

所以基本上你只需要删除

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"done == %@", @(NO)];
[fetchRequest setPredicate:predicate];

当然你还必须实现 – tableView:cellForRowAtIndexPath: 方法

关于ios - 显示包含核心数据的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15833892/

相关文章:

ios - 为什么Phonegap deviceready事件在很长一段时间后被触发?

ios - 如何在 appdelegate 中推送导航?

cocoa - NSManagedObject 未保存——谨防反向关系

ios - 给定 App ID,您如何确定 iOS 应用程序在应用商店(国家/地区)的可用性?

xcode - 在App Delegate文件中初始化Google Mobile Ads SDK的最新正确语法是什么?

ios - 在Xcode 6和Xcode 4.5之间切换

objective-c - 查找纬度和经度返回 0,0

ios - 获取核心数据最新 -id 并为新插入提供 -(+1)

iphone - 如何修复 ‘NSObjectInaccessibleException’ 获取请求在被 NSManagedObjectContext 使用之前无法响应 -entity'

iphone - iPhone上的自动数据备份