我是iPhone开发的新手,并且很难弄清楚我的桌子无法正常工作的原因。它可能是Core Data的东西,我不确定。 viewDidLoad方法在开始时工作正常,但是当我尝试滚动表视图时,当出现下一行时,我收到错误:
NSInvalidArgumentException',原因:' - [NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x5d52d70'
我的View Controller.h:
#import <UIKit/UIKit.h>
#define kTableViewRowHeight 66
@interface RostersViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSArray *objects;
}
@property(nonatomic,retain) NSArray *objects;
@end
我的View 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
感谢您的帮助。非常感谢。
答案 0 :(得分:5)
在viewDidLoad中将objects = [context executeFetchRequest:request error:&error];
更改为self.objects = [context executeFetchRequest:request error:&error];
executeFetchRequest返回一个自动释放的对象,然后将其直接存储到ivar中,这将在以后变为垃圾指针。这恰好最终指向一个字符串。
使用self.objects
使其使用合成的setter并保留它。