0

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

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

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

- (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


4

2 回答 2

1

如果我的代码正确,那么您已经创建了很好的部分。

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

static NSString *identifier = @"Entry";
UITableViewCell *cell = [inTableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
YourObject *cellEntry = [_fetchedResultsController objectAtIndexPath:indexPath];
//init cell
}

这应该为正确的部分选择正确的条目,否则您设置该部分的行是错误的。

索引路径选择正确的部分。

于 2013-04-05T12:39:45.353 回答
1

你的代码看起来几乎是正确的。由于您将 设置sectionKeyPath为“完成”,因此获取的结果控制器将自动为您创建部分。您可以为“完成”属性添加另一个排序描述符,以根据条目的部分对条目进行排序。

需要做的是删除NSPredicate. 在您当前的代码中,您使用“done == YES”过滤掉所有条目,但是由于您有两个部分,您希望结果控制器返回所有条目。

所以基本上你只需要删除

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

当然,您还必须实现该– tableView:cellForRowAtIndexPath:方法

于 2013-04-05T12:40:58.353 回答