2010-07-03 94 views
2

我是新來的iPhone開發,並一直在努力弄清楚爲什麼我的表不工作。這可能與Core Data有關,我不確定。 viewDidLoad方法在開始工作正常,但是當我嘗試滾動表視圖,當下一行出現,我得到的錯誤:NSCFString objectAtIndex無法識別的選擇器發送到實例0x5d52d70

NSInvalidArgumentException「的,理由是:」 - [NSCFString objectAtIndex:]:無法識別的選擇發送到實例0x5d52d70'

我查看或者Controller.h:

#import <UIKit/UIKit.h> 
#define kTableViewRowHeight 66 

@interface RostersViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource> { 

NSArray *objects; 
} 
@property(nonatomic,retain) NSArray *objects; 
@end 

我查看Controller.m或者:

#import "RostersViewController.h" 
#import "CogoAppDelegate.h" 
#import "TeamCell.h" 

@implementation RostersViewController 
@synthesize objects; 

- (void)viewDidLoad { 
CogoAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Teams"           inManagedObjectContext:context]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 

NSError *error; 

objects = [context executeFetchRequest:request error:&error]; 

if (objects == nil) 
{ 
    NSLog(@"There was an error!"); 
    // Do whatever error handling is appropriate 
} 
[request release]; 

UIApplication *app = [UIApplication sharedApplication]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationWillTerminate:) 
             name:UIApplicationWillTerminateNotification 
             object:app]; 
} 

- (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 { 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 


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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [objects count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:@"cell"]; 
id currObj = [objects objectAtIndex:indexPath.row]; 
cell.textLabel.text = [currObj valueForKey:@"name"]; 
return cell; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return kTableViewRowHeight; 
} 
@end 

感謝您的任何幫助。非常感謝。

回答

5

在viewDidLoad中變化objects = [context executeFetchRequest:request error:&error];self.objects = [context executeFetchRequest:request error:&error];

executeFetchRequest返回一個自動釋放的對象,該對象您然後直接存儲到所述的ivar,這變成在稍後的點垃圾指針。這恰好最終指向一個字符串。

使用self.objects使它使用合成的setter並保留它。

+0

Gotcha。它現在有效。謝謝你的幫助。 – quiksil12 2010-07-03 13:40:02

相關問題