2014-01-11 110 views
1

我在Xcode收到以下警告,並且不知道原因。歡迎任何幫助。NSFetchedResultsControllerDelegate警告在我的應用程序

Assigning to 'id<NSFetchedResultsControllerDelegate>' from incompatible type 'CollapsableTableViewViewController *' 

這是我的代碼,警告是在標有評論的行引發的。

#import "CollapsableTableViewViewController.h" 
#import "CollapsableTableView.h" 
#import "CollapsableTableViewAppDelegate.h" 


@interface CollapsableTableViewViewController() 

@end 


@implementation CollapsableTableViewViewController 
@synthesize fetchedResultsController = _fetchedResultsController; 

@synthesize managedObjectContext; 
/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //1 
    CollapsableTableViewAppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
    //2 
    self.managedObjectContext = appDelegate.managedObjectContext; 


     self.fetchedResultsController = nil; 









    CollapsableTableView* tableView = (CollapsableTableView*) myTableView; 
    tableView.collapsableTableViewDelegate = self; 

// [tableView setIsCollapsed:YES forHeaderWithTitle:@"First Section"]; 
// [tableView setIsCollapsed:YES forHeaderWithTitle:@"Second Section"]; 
// [tableView setIsCollapsed:YES forHeaderWithTitle:@"Third Section"]; 
// [tableView setIsCollapsed:YES forHeaderWithTitle:@"Fourth Section"]; 
// [tableView setIsCollapsed:YES forHeaderWithTitle:@"Fifth Section"]; 
} 


// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return YES; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

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


- (void)dealloc { 
    [spinner release]; 
    [super dealloc]; 
} 


#pragma mark - 
#pragma mark UITableViewDataSource 

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


+ (NSString*) titleForHeaderForSection:(int) section 
{ 
    switch (section) 
    { 
     case 0 : return @"Overdue"; 
     case 1 : return @"Today"; 
     case 2 : return @"Tomorrow"; 
     case 3 : return @"Upcoming"; 
     case 4 : return @"Someday"; 
     case 5 : return @"Completed"; 
     //default : return [NSString stringWithFormat:@"Section no. %i",section + 1]; 
    } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [CollapsableTableViewViewController titleForHeaderForSection:section]; 
} 

// Uncomment the following two methods to use custom header views. 
//- (UILabel *) createHeaderLabel: (UITableView *) tableView :(NSString *)headerTitle { 
// UILabel *titleLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
// titleLabel.frame =CGRectMake(0, 0, tableView.frame.size.width - 20, 60); 
// titleLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
// titleLabel.backgroundColor = [UIColor clearColor]; 
// titleLabel.textColor = [UIColor whiteColor]; 
// titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:20]; 
// titleLabel.text = headerTitle; 
// titleLabel.textAlignment = UITextAlignmentRight; 
// return titleLabel; 
//} 
// 
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
//{ 
// // create the parent view that will hold header Label 
// UIView * customView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.frame.size.width , 60)]autorelease]; 
// UILabel *titleLabel; 
// titleLabel = [self createHeaderLabel: tableView :[CollapsableTableViewViewController titleForHeaderForSection:section]]; 
//  
// [customView addSubview:titleLabel]; 
//  
// UILabel* collapsedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10,0,50,60)] autorelease]; 
// collapsedLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
// collapsedLabel.backgroundColor = [UIColor clearColor]; 
// collapsedLabel.textColor = [UIColor whiteColor]; 
// collapsedLabel.text = @"-"; 
// collapsedLabel.tag = COLLAPSED_INDICATOR_LABEL_TAG; 
// [customView addSubview:collapsedLabel]; 
//  
// customView.tag = section; 
// customView.backgroundColor = [UIColor blackColor]; 
// return customView; 
//} 

//- (NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 
//{ 
// return [NSString stringWithFormat:@"Footer %i",section + 1]; 
//} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int numero = [[self.fetchedResultsController sections] count]; 
    NSLog(@"numero = %d", numero); 
    switch (section) 
    { 
     case 2 : return numero; 
     case 3 : return 30; 
     default : return 8; 
    } 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 

    switch (indexPath.row) 
    { 
     case 0 : cell.textLabel.text = @"First Cell"; break; 
     case 1 : cell.textLabel.text = @"Second Cell"; break; 
     case 2 : cell.textLabel.text = @"Third Cell"; break; 
     case 3 : cell.textLabel.text = @"Fourth Cell"; break; 
     case 4 : cell.textLabel.text = @"Fifth Cell"; break; 
     case 5 : cell.textLabel.text = @"Sixth Cell"; break; 
     case 6 : cell.textLabel.text = @"Seventh Cell"; break; 
     case 7 : cell.textLabel.text = @"Eighth Cell"; break; 
     default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1]; 
    } 

    //cell.detailTextLabel.text = ...; 

    return cell; 
} 


#pragma mark - 
#pragma mark CollapsableTableViewDelegate 

- (void) collapsableTableView:(CollapsableTableView*) tableView willCollapseSection:(NSInteger) section title:(NSString*) sectionTitle headerView:(UIView*) headerView 
{ 
    [spinner startAnimating]; 
} 

- (void) collapsableTableView:(CollapsableTableView*) tableView didCollapseSection:(NSInteger) section title:(NSString*) sectionTitle headerView:(UIView*) headerView 
{ 
    [spinner stopAnimating]; 
} 

- (void) collapsableTableView:(CollapsableTableView*) tableView willExpandSection:(NSInteger) section title:(NSString*) sectionTitle headerView:(UIView*) headerView 
{ 
    [spinner startAnimating]; 
} 

- (void) collapsableTableView:(CollapsableTableView*) tableView didExpandSection:(NSInteger) section title:(NSString*) sectionTitle headerView:(UIView*) headerView 
{ 
    [spinner stopAnimating]; 
} 


#pragma mark - 
#pragma mark IBAction methods 

- (IBAction) toggleSection2 
{ 
    NSString* sectionTitle = //[NSString stringWithFormat:@"Tag %i",1]; // Use this expression when using custom header views. 
     [CollapsableTableViewViewController titleForHeaderForSection:1]; // Use this expression when specifying text for headers. 
    CollapsableTableView* collapsableTableView = (CollapsableTableView*) myTableView; 
    BOOL isCollapsed = [[collapsableTableView.headerTitleToIsCollapsedMap objectForKey:sectionTitle] boolValue]; 
    [collapsableTableView setIsCollapsed:! isCollapsed forHeaderWithTitle:sectionTitle]; 
} 

- (NSFetchedResultsController *)fetchedResultsController { 

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

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

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

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil 
                cacheName:nil]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; //the warning is on this line 

    return _fetchedResultsController; 

} 



@end 

這裏是我的頭文件:

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


@interface CollapsableTableViewViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,CollapsableTableViewDelegate> 
{ 
    IBOutlet UITableView* myTableView; 
    IBOutlet UIActivityIndicatorView *spinner; 


} 

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

- (IBAction) toggleSection2; 


@end 
+0

你的CollapsableTableViewViewController.h是什麼樣的? – valheru

+0

我將在我的問題中添加代碼... – mvasco

+1

相關[文檔](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html#//apple_ref/ doc/uid/TP40011210-CH11-SW3) – phi

回答

3

更換在點M檔的@interface符合@interface CollapsableTableViewViewController() <NSFetchedResultsControllerDelegate>解決您的問題。

你必須告訴fetchedResultsController「這個類符合NSFetchedResultsControllerDelegate協議」。

+0

在哪個文件? h或m? – mvasco

+0

兩個都沒關係,我認爲。我建議你將這個添加到點m文件中,因爲你可能不需要在點m文件之外分配這個CollapsableTableViewViewController對象。 –

+0

好了,完成並且警告消失了,我可以問你關於同一個文件的另一個問題嗎? – mvasco

相關問題