Ios7 核心数据NSFetchedResultsControllerDelegate问题

Ios7 核心数据NSFetchedResultsControllerDelegate问题,ios7,xcode5,Ios7,Xcode5,我目前正在尝试制作一款iPhone应用程序,但却遇到了一个小问题。在使用NSFetchedResultsControllerDelegate和使用NSFetchedResults控制器的任何地方,我都会遇到一个错误。当我在代码中实现核心数据时,我必须让它工作起来。欢迎任何反馈 代码如下: ClassTableViewController.h #import <UIKit/UIKit.h> #import "Classes.h" //@protocol NewClassViewCon

我目前正在尝试制作一款iPhone应用程序,但却遇到了一个小问题。在使用NSFetchedResultsControllerDelegate和使用NSFetchedResults控制器的任何地方,我都会遇到一个错误。当我在代码中实现核心数据时,我必须让它工作起来。欢迎任何反馈

代码如下:

ClassTableViewController.h

#import <UIKit/UIKit.h>
#import "Classes.h"

//@protocol NewClassViewControllerDelegate;


@interface NewClassViewController : UIViewController

//@property (weak, nonatomic) id <NewClassViewControllerDelegate> delegate;

@property (weak, nonatomic) IBOutlet UITextField *classTextField;
@property (weak, nonatomic) IBOutlet UITextField *periodTextField;
@property (strong, nonatomic) Classes *classID;

- (IBAction)saveClass:(id)sender;

@end
#导入
#导入“Classes.h”
//@协议NewClassViewControllerDelegate;
@接口NewClassViewController:UIViewController
//@属性(弱、非原子)id委托;
@属性(弱,非原子)IBOutlet UITextField*classTextField;
@属性(弱,非原子)IBUITextField*periodTextField;
@属性(强,非原子)类*classID;
-(iAction)存储类:(id)发送方;
@结束
ClassTableViewController.m

#import "ClassTableViewController.h"
#import "Classes.h"
#import "ClassTableViewCell.h"
#import "NewClassViewController.h"

@interface ClassTableViewController ()
- (void)configureCell:(ClassTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

@implementation ClassTableViewController


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - Segue

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ( [[segue identifier] isEqualToString:@"ShowClass"] ){
        NewClassViewController *dvc = (NewClassViewController *)[segue destinationViewController];
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Classes *classID = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        [dvc setClassID:classID];

    } else if  ([[segue identifier] isEqualToString:@"addClass"]) {
        NewClassViewController *dvc = (NewClassViewController *)[[segue destinationViewController] topViewController];
        [dvc setClassID:[NSEntityDescription insertNewObjectForEntityForName:@"Classes" inManagedObjectContext:self.managedObjectContext]];
    }

}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(firstDone)];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) firstDone {

    [self.navigationController  dismissModalViewControllerAnimated:YES];
}


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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    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 = @"ClassesCell";

    ClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ClassTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(ClassTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Classes *classID = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.classLabel.text = classID.classTitle;
    cell.periodLabel.text = classID.period;
}

#pragma mark - Fetched results controller

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


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Classes  " inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"period" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    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.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;


}

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


- (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;
    }
}


- (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:
            [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.tableView endUpdates];
}

/*
// 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
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// 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
#导入“ClassTableViewController.h”
#导入“Classes.h”
#导入“ClassTableViewCell.h”
#导入“NewClassViewController.h”
@接口ClassTableViewController()
-(void)configureCell:(ClassTableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath;
@结束
@实现类TableViewController
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
#布拉格马克塞格
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
if([[segue identifier]IsequalString:@“ShowClass”]){
NewClassViewController*dvc=(NewClassViewController*)[segue destinationViewController];
NSIndexPath*indexPath=[self.tableView indexPathForSelectedRow];
Classes*classID=[[self-fetchedResultsController]objectAtIndexPath:indexPath];
[dvc setClassID:classID];
}else if([[segue identifier]IsequalString:@“addClass”]){
NewClassViewController*dvc=(NewClassViewController*)[[segue destinationViewController]topViewController];
[dvc setClassID:[NSEntityDescription insertNewObjectForEntityForName:@“Classes”inManagedObjectContext:self.managedObjectContext];
}
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarbuttonSystemDone目标:自我操作:@selector(firstDone)];
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(无效)先完成{
[self.navigationController dismissModalViewControllerAnimated:是];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“ClassesCell”;
ClassTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[ClassTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
-(void)configureCell:(ClassTableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath{
Classes*classID=[self.fetchedResultsController对象索引路径:indexPath];
cell.classLabel.text=classID.classTitle;
cell.periodlab.text=classID.period;
}
#pragma标记-获取的结果控制器
-(NSFetchedResultsController*)fetchedResultsController
{
如果(u fetchedResultsController!=nil){
返回uu fetchedResultsController;
}
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“类”在managedObjectContext:self.managedObjectContext];
[FetchRequestSetEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“期间”升序:是];
NSArray*sortDescriptor=[NSArray arrayWithObjects:sortDescriptor,nil];
[FetchRequestSetSortDescriptors:sortDescriptors];
NSFetchedResultsController*aFetchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@“Master”];
aFetchedResultsController.delegate=self;
self.fetchedResultsController=aFetchedResultsController;
n错误*错误=nil;
如果(![self.fetchedResultsController性能检测:&错误]){
/*
将此实现替换为适当处理错误的代码。
abort()导致应用程序生成崩溃日志并终止。您不应该在装运应用程序中使用此函数,尽管它在开发过程中可能很有用。
*/
NSLog(@“未解决的错误%@,%@”,错误,[error userInfo]);
中止();
}
返回uu fetchedResultsController;
}
-(void)controllerWillChangeContent:(NSFetchedResultsController*)控制器{
[self.tableView开始更新];
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(NSUITE)
- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
        entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey:@"details.closeDate" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext sectionNameKeyPath:nil
            cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}