我遇到了将数据放入表视图的问题。这是我目前的代码。 我无法从我尝试使用NSFetchedResultsController的类加载结果,但它不起作用。任何人都可以看到错误。 这是头文件。
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <sqlite3.h>
#import "FlickrFetcher.h"
@interface PersonList : UITableViewController {
FlickrFetcher *fetcher;
NSArray *nameList;
NSArray *names;
NSFetchedResultsController *results;
NSArray *photos;
}
@property (retain, nonatomic)NSArray *nameList;
@property (retain, nonatomic)NSArray *names;
@property (retain, nonatomic)NSArray *photos;
@property (retain, nonatomic)NSFetchedResultsController *results;
@end
这是.m文件。
#import "PersonList.h"
@implementation PersonList
@synthesize nameList,names,results,photos;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.title=@"Contacts";
// Custom initialization
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
fetcher = [FlickrFetcher sharedInstance];
NSString *path=[[NSBundle mainBundle]pathForResource:@"FakeData" ofType:@"plist"];
NSArray *array=[NSArray arrayWithContentsOfFile:path];
NSManagedObjectContext *context=[fetcher managedObjectContext];
if([fetcher databaseExists]==YES){
for(NSDictionary *dic in array){
PersonList *person=(PersonList *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
[person setNameList:[dic objectForKey:@"user"]];
[person setPhotos:[dic objectForKey:@"path"]];
names=[fetcher fetchManagedObjectsForEntity:@"Person" withPredicate:nil];
results=[fetcher fetchedResultsControllerForEntity:@"Person" withPredicate:nil];
}
}
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.names=nil;
}
- (void)dealloc {
[fetcher release];
[nameList release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[results sections] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"NOT REACHED HERE");
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[results sections] objectAtIndex:row];
return cell;
}
@end
这是FlickrFetcher方法的方法
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface FlickrFetcher : NSObject {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
// Returns the 'singleton' instance of this class
+ (id)sharedInstance;
// Checks to see if any database exists on disk
- (BOOL)databaseExists;
// Returns the NSManagedObjectContext for inserting and fetching objects into the store
- (NSManagedObjectContext *)managedObjectContext;
// Returns an array of objects already in the database for the given Entity Name and Predicate
- (NSArray *)fetchManagedObjectsForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate;
// Returns an NSFetchedResultsController for a given Entity Name and Predicate
- (NSFetchedResultsController *)fetchedResultsControllerForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate;
@end
答案 0 :(得分:10)
[super viewDidLoad]
。只有在您的代码位于-dealloc
方法之后才能调用super。NSFetchedResultsController
并对其执行任何操作。你甚至没有参考。 这是不正确的。您应该每NSFetchedResultsController
一个 UITableViewController
;你应该保留对它的引用;您的UITableViewDatasource
(在这种情况下是您的UITableViewController
)应该是其代表。-performFetch:
上调用NSFetchedResultsController
,因此没有检索到数据。NSFetchedResultsController
的代表,所以无论如何都无法知道它何时返回数据,因为这是传达数据更改的唯一方式。我会重新考虑您的设计并再次审核Core Data iPhone示例项目。
NSFetchedResultsController
在您保存NSManagedObjectContext
之前不会触发其委托方法,这就是您没有看到数据的原因。
至于您获得的错误,您正在尝试在不响应该属性的对象上设置属性。我建议将您的应用程序加载到调试器中,在objc_exception_throw
上放置一个断点,并查看导致问题的操作对象。很可能你会找回一个物体并认为它是另一个物体。