0
votes

so I'm working on a litte ToDo app. The TableView shows a CoreData Entity with the attributes of the name(string) and the Date(Date). Currently the NSfetchedResultsController sort the tableView by the Date of the ToDo, but i also want some sections for example "expired ToDo's - which have a date in the past" or "ToDo's for the next week"

how can I achieve this?

Code NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    // Create and configure a fetch request with the Book entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Inventory" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *productDescriptor = [[NSSortDescriptor alloc] initWithKey:@"inventoryProductName" ascending:YES];
    NSSortDescriptor *dateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"expireDate" ascending:YES];
    NSArray *sortDescriptors = @[dateDescriptor,productDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"inventoryProductName" cacheName:@"Root"];
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

Code TableView:

- (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];
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    Inventory *inventory = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = inventory.inventoryProductName;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    NSDate *dt = inventory.expireDate;
    NSString *dateAsString = [formatter stringFromDate:dt];
    //[formatter release];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Expires at: %@", dateAsString];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];
    return cell;

}
1

1 Answers

0
votes

You need to give your todo items another property that reflects what you want. As for "past", "next week" you could use a transient property that is calculated on the fly via a custom getter. In some cases, you will have to actually persist this as a (non-transient) attribute for your fetched results controller to work.

For predictable sorting, the actual attribute value could just be a number - this would allow you to also include date-independent categories, such as "canceled", "invalid", etc.