2012-04-17 2 views
0

Я пытаюсь реализовать UIViewController, который имеет 2 UITableViews (на iPad): один для отображения разделов контрольного списка, а другой - для отображения вопросов в выбранном/разделе.UITableView и делегаты NSFetchedController никогда не вызывали

Я установил NSFetchedResultsController, и я могу успешно извлекать объекты из постоянного хранилища, однако ни один из методов делегата NSFetchedResultsController или UITableView не вызывается.

Я задал класс для реализации соответствующих протоколов и назначил делегатам FRC и TableView «self».

Я поставил точки останова в каждом из методов делегата, но никогда не достигал ни одного из них (да, я работаю с включенными точками останова и могу просто пройти через другие части кода).

Я ценю любое представление о предлагаемых, почему делегат методы не будет называться в этих случаях: 1), когда [FRC performFetch] исполняет и FRC.fetchedObjects устанавливается/обновляется 2), когда [Tableview reloadData] называется (должны, по крайней мере, проверить numberOfSectionsInTableView)

.H Файл:

#import <UIKit/UIKit.h> 
#import <CoreData/CoreData.h> 


@interface ChecklistViewController : UIViewController <NSFetchedResultsControllerDelegate, 
                 UITableViewDelegate, 
                 UITableViewDataSource> 
{ 
    UIView *sectionsView; 

    UITableViewController  *sectionsTable; 
    NSFetchedResultsController *sectionsFetchedResultsController; 
} 

@property (nonatomic, retain) IBOutlet UIView      *sectionsView; 
@property (nonatomic, retain) IBOutlet UITableViewController  *sectionsTable; 
@property (nonatomic, retain)   NSFetchedResultsController *sectionsFetchedResultsController; 

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

@end 

.M FILE

#import "ChecklistViewController.h" 
#import "Inspection.h" 
#import "InspectionQuestion.h" 
#import "ContextManager.h" 


@implementation ChecklistViewController 

@synthesize sectionsView; 
@synthesize sectionsTable; 
@synthesize sectionsFetchedResultsController; 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 



#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Setup tableviews 
    self.sectionsTable = [[UITableViewController alloc] init]; 
    self.sectionsTable.view = self.sectionsView; 
    self.sectionsTable.tableView.dataSource = self;   
    self.sectionsTable.tableView.delegate = self;  

    //Fetch sections 
    NSError *error = nil; 
    if (self.sectionsFetchedResultsController.fetchedObjects == nil) { 
     self.sectionsFetchedResultsController.delegate = self; 
     if (![[self sectionsFetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     }  
    } 
    NSLog(@"fetched results:%@",self.sectionsFetchedResultsController.fetchedObjects); 

    //For testing delegates 
    [self.sectionsTable.tableView reloadData]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 


#pragma mark - 
#pragma mark TableViewDelegate methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSInteger count = [[self.sectionsFetchedResultsController sections] count]; 

    if (count == 0) { 
     count = 1; 
    } 

    return count; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSInteger numberOfRows = 0; 

    if ([[self.sectionsFetchedResultsController sections] count] > 0) { 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.sectionsFetchedResultsController sections] objectAtIndex:section]; 
     numberOfRows = [sectionInfo numberOfObjects]; 
    } 

    NSLog(@"CheckListViewController::numberOfRowsInSection - numberOfRows:%d", numberOfRows); 
    return numberOfRows; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Dequeue or if necessary create a TableViewCell, then set its to the for the current row.   
    static NSString *cellIdentifier = @"cellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    // Configure the cell 
    NSLog(@"CheckListViewController::configureCell - indexPath:%@,", indexPath);   
    InspectionQuestion *question = [self.sectionsFetchedResultsController objectAtIndexPath:indexPath];  
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"CheckListViewController::didSelectRowAtIndexPath - started"); 
    InspectionQuestion *question = [self.sectionsFetchedResultsController objectAtIndexPath:indexPath];  
    //[self show: animated:YES]; 
    NSLog(@"CheckListViewController::didSelectRowAtIndexPath - ended"); 
} 

#pragma mark - 
#pragma mark FetchedResultsController 

- (NSFetchedResultsController *)sectionsFetchedResultsController { 
    // Set up the fetched results controller if needed. 
    NSLog(@"ChecklistViewController::sectionsFetchedResultsController - started"); 

    if (sectionsFetchedResultsController == nil) { 

     NSLog(@"ChecklistViewController::fetchedResultsController - FETCHING new results");  

     NSManagedObjectContext *managedObjectContext = [[ContextManager sharedContext] managedObjectContext];  

     // Create the fetch request for the entity. 
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

     NSEntityDescription *entity = [NSEntityDescription entityForName:@"InspectionQuestion" inManagedObjectContext:managedObjectContext]; 
     [fetchRequest setEntity:entity]; 

     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sectionId" ascending:YES]; 
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
     [fetchRequest setSortDescriptors:sortDescriptors]; 


     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil 
      cacheName:nil 
//     cacheName:@"Root" 
     ]; 

     //aFetchedResultsController.delegate = self; 
     self.sectionsFetchedResultsController = aFetchedResultsController; 
//  self.sectionsFetchedResultsController.delegate = self; 

/* ARC 
     [aFetchedResultsController release]; 
     [fetchRequest release]; 
     [sortDescriptor release]; 
     [sortDescriptors release]; 
*/ 
    } 

    return sectionsFetchedResultsController; 
} 


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    if (controller == self.sectionsFetchedResultsController) { 
     [self.sectionsTable.tableView beginUpdates]; 
    } 
} 


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

    if (controller == self.sectionsFetchedResultsController) { 
     tableView = self.sectionsTable.tableView; 
    }  

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      NSLog(@"ChecklistViewController::didChangeObject - INSERT"); 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      NSLog(@"ChecklistViewController::didChangeObject - DELETE"); 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      NSLog(@"ChecklistViewController::didChangeObject - UPDATE"); 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      NSLog(@"ChecklistViewController::didChangeObject - MOVE"); 
      [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.sectionsTable.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.sectionsTable.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.sectionsTable.tableView endUpdates]; 
} 


@end 

Благодаря

ответ

1

попытка изменения UITableViewController к UITableView

UITableView  *sectionsTable; 

и сделать тип вашего UIViewControllerUITableViewController

@interface ChecklistViewController : UITableViewController <NSFetchedResultsControllerDelegate, 
                UITableViewDelegate, 
                UITableViewDataSource> 
+0

я попытался сделать в ViewController UITableViewController, но получить «NSInternalInconsistencyException» с разумом «[UITableViewController loadView] загрузил„ChecklistViewController“перо, но не получил UITableView.'then он ожидал NIB быть – econstantin

+0

I попытался сделать ViewController UITableViewController, но затем получить «NSInternalInconsistencyException» во время выполнения с причиной «[UITableViewController loadView] загрузил« ChecklistViewController », но не получил UITableView». Поскольку представление будет иметь несколько таблиц, а также другие представления/контроллеры, я считаю, что это должен быть UIViewController. Есть ли опция отладки, которая позволила бы регистрировать то, что делает iOS под капотом относительно вызова методов делегата? – econstantin

+0

сделал не знаю, что там, где два табличных представления. не пробовали это самостоятельно, но, например, есть вопросы в SO с этой проблемой: http://stackoverflow.com/questions/6519673/ios-tableview-delegate-methods-for-two- TableView – Pfitz

0

Я нашел вопрос - UITableViewController не был должным образом добавляется к просмотру иерархия

Вместо

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Setup tableviews 
    self.sectionsTable = [[UITableViewController alloc] init]; 
    self.sectionsTable.view = self.sectionsView; 
    self.sectionsTable.tableView.dataSource = self;   
    self.sectionsTable.tableView.delegate = self;  

должно быть

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Setup tableviews 
    self.sectionsTable = [[UITableViewController alloc] init];  
    [self.sectionsView addSubview:self.sectionsTable.view]; 
    self.sectionsTable.tableView.dataSource = self;   
    self.sectionsTable.tableView.delegate = self;  

Примечание: рама для sectionsTable.view все еще должен быть установлен.

В настоящее время вызывается методы делегата tableview и FRC, а рендеринг tableview.

Смежные вопросы