NSFetchedResultsController titleForHeaderInSection with formatted NSDate

前端 未结 3 1560
一生所求
一生所求 2021-02-10 10:22

In my Core Data app I am using a FetchedResultsController. Usually to set titles for headers in a UITableView you would implement the following method like so:

-         


        
3条回答
  •  无人及你
    2021-02-10 10:27

    I found a solution:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        //Returns the title for each section header. Title is the Date.
        id  sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
        NSArray *objects = [sectionInfo objects];
        NSManagedObject *managedObject = [objects objectAtIndex:0];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        NSDate *headerDate = (NSDate *)[managedObject valueForKey:@"itemDate"];
        NSString *headerTitle = [formatter stringFromDate:headerDate];
        [formatter release];
        return headerTitle;
    }
    

    Please look over this, if you know of a better way please say!

    Otherwise if your stuck with a similar problem I hope this helps!

提交回复
热议问题