2014-01-15 25 views
0

我需要在節標題中包含行數,如TODAY(6),這意味着在標題爲TODAY的節中有6行(在我的情況下是核心數據對象)。我知道它是在這裏找到:將行數放在節標題中

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

但我不知道如何將這包括在每個部分顯示爲標籤文本。 任何意見是值得歡迎....

#import "ToDoItemsTableViewController.h" 
#import "AppDelegate.h" 
#import "AddToDoItemViewController.h" 
#import "ToDoSubItemsTableViewController.h" 

@interface ToDoItemsTableViewController() 

@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController; 

@end 

@implementation ToDoItemsTableViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
-(NSManagedObjectContext *)managedObjectContext{ 
    return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext]; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSError *error = nil; 
    if (![[self fetchedResultsController]performFetch:&error]){ 
     NSLog(@"Error %@",error); 
     abort(); 
    } 
} 
-(void) viewWillAppear:(BOOL)animated{ 
    [self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    if ([[segue identifier]isEqualToString:@"addToDoItem"]){ 
     UINavigationController *navigationController = segue.destinationViewController; 

     AddToDoItemViewController *addToDoItemViewController = (AddToDoItemViewController*)navigationController.topViewController; 
     ToDoItem *addToDoItem = [NSEntityDescription insertNewObjectForEntityForName:@"ToDoItem" inManagedObjectContext:self.managedObjectContext]; 
     addToDoItemViewController.addToDoItem = addToDoItem; 
    } 
    if ([[segue identifier] isEqualToString:@"toToDoSubItems"]){ 

     ToDoSubItemsTableViewController *todoSubItemsTableViewController = [segue destinationViewController]; 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     ToDoItem *selectedToDoItem = (ToDoItem*)[self.fetchedResultsController objectAtIndexPath:indexPath]; 
     todoSubItemsTableViewController.selectedToDoItem = selectedToDoItem; 



    } 




} 



#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return [[self.fetchedResultsController sections]count]; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    ToDoItem *todoItem = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = todoItem.todoName; 



    NSDate *fechaToDO = todoItem.todoDueDate; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setDateFormat:@"EEEE, dd MMMM YYYY"]; 
    NSString *fechaToDo = [dateFormatter stringFromDate:fechaToDO]; 



    cell.detailTextLabel.text = fechaToDo; 
    return cell; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *header = @"customHeader"; 

    UITableViewHeaderFooterView *vHeader; 

    vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header]; 

    if (!vHeader) { 
     vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header]; 
     vHeader.textLabel.backgroundColor = [UIColor redColor]; 
     vHeader.textLabel.textColor = [UIColor whiteColor]; 

     vHeader.contentView.backgroundColor = [UIColor redColor]; 
    } 

    if (section == 1) { 
     vHeader.textLabel.backgroundColor = [UIColor blueColor]; 
     vHeader.textLabel.textColor = [UIColor whiteColor]; 

     vHeader.contentView.backgroundColor = [UIColor blueColor]; 
    } else { 
     [vHeader setBackgroundColor:[UIColor redColor]]; 
    } 

    vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section]; 

    return vHeader; 
} 
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 




    id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section]; 
    NSString *sectionname = [theSection name]; 

    if ([sectionname isEqualToString:@"0"]){ 
     return @"Overdue"; 

    } 
    else if ([sectionname isEqualToString:@"1"]){ 
     return @"Today"; 
    } 
    else if ([sectionname isEqualToString:@"2"]){ 
     return @"Upcoming"; 
    } 




    if ([[self.fetchedResultsController sections]count]>0){ 
     id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section]; 
     return [sectionInfo name]; 
    } 
    else{ 
     return nil; 
    } 

} 


#pragma mark - Fetched Results Controller Section 

-(NSFetchedResultsController*)fetchedResultsController{ 

    if (_fetchedResultsController != nil){ 
     return _fetchedResultsController; 
    } 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; 
    NSManagedObjectContext *context = self.managedObjectContext; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ToDoItem" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"todoDueDate" ascending:YES]; 
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"todoName" ascending:YES]; 

    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor,sortDescriptor1, nil]; 
    fetchRequest.sortDescriptors = sortDescriptors; 
    _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionIdentifier" cacheName:nil]; 
    _fetchedResultsController.delegate = self; 
    return _fetchedResultsController; 
} 


#pragma mark - Fetched Results Controller Delegates 

-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 

} 

-(void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{ 

    UITableView *tableView = self.tableView; 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeUpdate:{ 

      ToDoItem *changeToDoItem = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
      cell.textLabel.text = changeToDoItem.todoName; 
      cell.detailTextLabel.text = changeToDoItem.todoDescription; 
     } 
      break; 
     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 


} 

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{ 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 

} 





/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 


// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     NSManagedObjectContext *context = [self managedObjectContext]; 
     ToDoItem *ToDoItemToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     [context deleteObject:ToDoItemToDelete]; 


     NSError *error = nil; 
     if (![context save:&error]){ 
      NSLog(@"Error: %@",error); 
     } 

    } 
} 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

/* 
#pragma mark - Navigation 

// In a story board-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 

*/ 

@end 

回答

1
[NSString stringWithFormat:@"Today(%d)", [tableView numberOfRowsInSection: 
    section]]; 
+0

查詢表視圖,而不是直接,[self.tableView numberOfRowsInSection:段] – SomeGuy

+1

@SomeGuy是啊,我不知道我在想什麼。忘記可以在UITableView上調用'numberOfRowsInSection',但不應該使用'self.tableView',因爲這很可能是在發送'tableView'作爲參數的數據源方法中調用的。 – nhgrif

+0

謝謝你們倆... @ SomeGuy .. – mvasco