gpt4 book ai didi

ios - UITableView 和 NSFetchedController 委托(delegate)从未被调用

转载 作者:行者123 更新时间:2023-11-29 04:42:22 25 4
gpt4 key购买 nike

我正在尝试实现一个 UIViewController,它有 2 个 UITableView(在 iPad 上):一个用于显示 list 的各个部分,另一个用于显示所选/部分中的问题。

我已经设置了 NSFetchedResultsController 并且能够成功从持久存储中获取对象,但是没有调用任何 NSFetchedResultsController 或 UITableView 委托(delegate)方法。

我确实设置了类来实现相应的协议(protocol),并将 FRC 和 TableView 委托(delegate)设置为“self”。

我已经在每个委托(delegate)方法中放置了断点,但从未到达其中任何一个(是的,我正在启用断点的情况下运行,并且可以很好地单步执行代码的其他部分)。

我很欣赏任何关于为什么在这些情况下不会调用委托(delegate)方法的见解: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 文件

#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

谢谢

最佳答案

尝试将UITableViewController更改为UITableView

UITableView       *sectionsTable;

并将 UIViewController 类型设置为 UITableViewController

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

关于ios - UITableView 和 NSFetchedController 委托(delegate)从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com