Iphone 未调用FetchedResultsController

Iphone 未调用FetchedResultsController,iphone,objective-c,fetch,Iphone,Objective C,Fetch,我的viewController中有标准的fetchedResultsController方法和委托方法,但没有调用它们。我通过在这个方法中放入一个NSLog来测试这个问题,而控制台从来没有显示过这一点 我确保将FetchedResultsController委托添加到.h和.m 你知道为什么吗 接口 @interface LogViewController : UIViewController <NSFetchedResultsControllerDelegate, UITableVi

我的viewController中有标准的fetchedResultsController方法和委托方法,但没有调用它们。我通过在这个方法中放入一个NSLog来测试这个问题,而控制台从来没有显示过这一点

我确保将FetchedResultsController委托添加到.h和.m

你知道为什么吗

接口

 @interface LogViewController : UIViewController <NSFetchedResultsControllerDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSArray *logArray;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) Session *session;

@property (nonatomic, retain) IBOutlet UITableView *logTableView;

@end
@接口LogViewController:UIViewController
@属性(非原子,保留)NSFetchedResultsController*fetchedResultsController;
@属性(非原子,保留)NSManagedObjectContext*managedObjectContext;
@属性(非原子,保留)NSArray*logArray;
@属性(非原子,保留)UIImageView*imageView;
@属性(非原子,保留)会话*会话;
@属性(非原子,保留)IBUITableView*logTableView;
@结束
实施

#import "LogViewController.h"

@implementation LogViewController

@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext;
@synthesize logArray;
@synthesize logTableView;
@synthesize imageView;
@synthesize session;

- (void)dealloc
{
    [logArray release];
    [logTableView release];
    [session release];
    [__fetchedResultsController release];
    [managedObjectContext release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Log";
    logTableView.backgroundColor = [UIColor clearColor];
    logTableView.separatorColor = [UIColor blackColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
    self.logArray = [[NSArray alloc]initWithObjects:@"Today's Workout", @"Last Workout", @"Past Week", @"Past Month", @"All Workouts", nil];

    if (managedObjectContext == nil)
    {
        self.managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }
}

- (void)viewDidUnload
{
    self.logArray = nil;
    self.logTableView = nil;
    [super viewDidUnload];
}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.logArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.textColor = [UIColor blackColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [logArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    UIImageView *myImageView = nil;
    if (indexPath.row == 0)
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_top.png"]];
    }
    else if (indexPath.row == 4)
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_bottom.png"]];
    }
    else
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_middle.png"]];
    }
    [cell setBackgroundView:myImageView];
    [myImageView release];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d, y"];
    NSDate *date = nil;
    if (indexPath.row == 0)
    {
        date = [NSDate date];
        NSString *dateString = [dateFormatter stringFromDate:date];
        cell.badgeString = dateString;
    }
    else if (indexPath.row == 1)
    {
        self.session = [[__fetchedResultsController fetchedObjects]lastObject];
        NSDate *date = self.session.timeStamp;
        NSString *dateString = [dateFormatter stringFromDate:date];
        cell.badgeString = dateString;
    }
    else if (indexPath.row > 1)
    {
        cell.badgeString = [NSString stringWithFormat:@"%i", [[__fetchedResultsController fetchedObjects]count]];
    }
    cell.badgeColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
    [dateFormatter release];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// [cell setBackgroundColor:[UIColor colorWithRed:209/255.0 green:209/255.0 blue:209/255.0 alpha:1.0]];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0 || indexPath.row == 1)
    {
        SessionViewController *detailViewController = [[SessionViewController alloc] initWithNibName:@"SessionViewController" bundle:nil];
        detailViewController.title = [logArray objectAtIndex: indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
    else
    {
        LogResultsViewController *detailViewController = [[LogResultsViewController alloc] initWithNibName:@"LogResultsTableViewController" bundle:nil];
        detailViewController.title = [logArray objectAtIndex: indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
    {
        return __fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
        /*
        Replace this implementation with code to handle the error appropriately.
        abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
        */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return __fetchedResultsController;
    NSLog(@"Number of Objects = %i",
          [[__fetchedResultsController fetchedObjects] count]);

}

#pragma mark - Fetched results controller delegate


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

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.logTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

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

    switch(type)
    {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

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

@end
#导入“LogViewController.h”
@实现日志视图控制器
@综合fetchedResultsController=\uu fetchedResultsController;
@综合语境;
@综合对数阵列;
@综合日志表视图;
@综合图像视图;
@综合会议;
-(无效)解除锁定
{
[日志阵列发布];
[logTableView发布];
[会议发布];
[[uuu fetchedResultsController发布];
[managedObjectContext发布];
[super dealoc];
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
self.navigationItem.title=@“日志”;
logTableView.backgroundColor=[UIColor clearColor];
logTableView.separatorColor=[UIColor blackColor];
self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:24/255.0绿色:83/255.0蓝色:170/255.0 alpha:1.0];
self.logArray=[[NSArray alloc]initWithObjects:@“今天的训练”、“最后一次训练”、“过去一周”、“过去一个月”、“所有训练”,无];
if(managedObjectContext==nil)
{
self.managedObjectContext=[(CurlAppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
}
}
-(无效)视图卸载
{
self.logArray=nil;
self.logTableView=nil;
[超级视频下载];
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.logArray count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
TDBadgedCell*单元格=[[[TDBadgedCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier]自动释放];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:CellIdentifier]自动释放];
}
cell.textLabel.textColor=[UIColor blackColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textlab.text=[logArray objectAtIndex:indexath.row];
cell.backgroundColor=[UIColor clearColor];
UIImageView*myImageView=nil;
if(indexPath.row==0)
{
myImageView=[[UIImageView alloc]initWithImage:[UIImageName:@“customcell_background_top.png”];
}
else if(indexPath.row==4)
{
myImageView=[[UIImageView alloc]initWithImage:[UIImageName:@“customcell_background_bottom.png”];
}
其他的
{
myImageView=[[UIImageView alloc]initWithImage:[UIImageName:@“customcell_background_middle.png”];
}
[单元格设置背景视图:myImageView];
[myImageView发布];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@“MMM d,y”];
NSDate*日期=零;
if(indexPath.row==0)
{
日期=[NSDate date];
NSString*dateString=[dateFormatter stringFromDate:date];
cell.badgeString=日期字符串;
}
else if(indexPath.row==1)
{
self.session=[[uuu fetchedResultsController fetchedObjects]lastObject];
NSDate*date=self.session.timeStamp;
NSString*dateString=[dateFormatter stringFromDate:date];
cell.badgeString=日期字符串;
}
else if(indexPath.row>1)
{
cell.badgeString=[NSString stringWithFormat:@“%i”,[[uu fetchedResultsController fetchedObjects]计数]];
}
cell.badgeColor=[UIColor colorWithRed:24/255.0绿色:83/255.0蓝色:170/255.0 alpha:1.0];
[日期格式化程序发布];
返回单元;
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(NSIndexPath*)indexPath
{
//[单元格设置背景颜色:[UIColor COLOR WITH RED:209/255.0绿色:209/255.0蓝色:209/255.0 alpha:1.0];
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[tableView取消行索引路径:indexPath动画:是];
if(indexPath.row==0 | | indexPath.row==1)
{
SessionViewController*detailViewController=[[SessionViewController alloc]initWithNibName:@“SessionViewController”捆绑包:nil];
detailViewController.title=[logArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController动画:是];
[详细视图控制器发布];
}
其他的
{
LogResultsViewController*detailViewController=[[LogResultsViewController alloc]initWithNibName:@“LogResultsTableViewController”捆绑包:nil];
detailViewController.title=[logArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController动画:是];
[详细视图控制器发布];
}
}
#pragma标记-获取的结果控制器
-(NSFetchedResultsController*)fetchedResultsController
{
如果(u fetchedResultsController!=nil)
{
返回uu fetchedResultsController;
}
//为实体创建获取请求。
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
//
self.session = [[__fetchedResultsController fetchedObjects]lastObject];
self.session = [[self.fetchedResultsController fetchedObjects]lastObject];