我一直在這一個特定的問題上敲了我的頭大約2個小時,現在閱讀文檔和示例,我只是無法環繞它。我擁有核心數據模型Person和Photo,並且它們已鏈接。我試圖顯示一個UITableView的內容和from NSFetchedResults objectAtIndex:0返回0
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
保持返回0.下面是相關的代碼。 我知道我的核心數據中有數據,因爲我可以在終端中用sqlite查看它。
AppDelegate.h
@interface PaparazziAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *tabController;
UINavigationController *mainNavController;
UINavigationController *recentsNavController;
PersonListViewController *personListView;
PhotoListViewController *recentsList;
PhotoDetailViewController *photoDetail;
FlickrFetcher *fetcher;
NSManagedObjectContext *currentContext;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabController;
@property (nonatomic, retain) IBOutlet UINavigationController *mainNavController, *recentsNavController;
@end
AppDelegate.m
#import "PaparazziAppDelegate.h"
@implementation PaparazziAppDelegate
@synthesize window;
@synthesize tabController, mainNavController, recentsNavController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Setup the database instances for access.
fetcher = [FlickrFetcher sharedInstance];
// Check to see if the database exists, if not, load the plist and populate.
if ([fetcher databaseExists])
NSLog(@"I'm here!");
else {
NSLog(@"No database yet!");
currentContext = [fetcher managedObjectContext];
/* -- REMOVED FOR READABILITY --
A chunk of code for creating fresh data from a plist if the database doesn't yet exist.
I know this part works because I can see the data using the terminal */
[currentContext release];
}
// End of the plist loading and database creation.
personListView = [[PersonListViewController alloc] initWithStyle:UITableViewStylePlain];
personListView.title = @"Contacts";
[mainNavController pushViewController:personListView animated:NO];
[personListView release];
recentsList = [[PhotoListViewController alloc] initWithNibName:@"PhotoListViewController" bundle:[NSBundle mainBundle]];
recentsList.title = @"Recents";
[recentsNavController pushViewController:recentsList animated:NO];
[recentsList release];
// setting up view icons
UITabBarItem *contactsIcon = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
mainNavController.tabBarItem = contactsIcon;
[contactsIcon release];
UITabBarItem *recentsIcon = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemRecents tag:0];
recentsNavController.tabBarItem = recentsIcon;
[recentsIcon release];
[self.window addSubview:tabController.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[window release];
[tabController release];
[mainNavController release];
[recentsNavController release];
[fetcher release];
[super dealloc];
}
@end
PersonListViewController.h
#import <UIKit/UIKit.h>
#import "FlickrFetcher.h"
#import "Person.h"
#import "Photo.h"
@interface PersonListViewController : UITableViewController {
NSArray *listOfPeople;
FlickrFetcher *fetcher;
NSManagedObjectContext *currentContext;
NSFetchedResultsController *fetchedResultsController;
}
@property (nonatomic, retain) NSArray *listOfPeople;
@property (nonatomic, retain) NSManagedObjectContext *currentContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@end
而且麻煩的文件,PersonListViewController.m
#import "PersonListViewController.h"
@implementation PersonListViewController
@synthesize listOfPeople, currentContext, fetchedResultsController;
#pragma mark -
#pragma mark Initialization
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
fetcher = [FlickrFetcher sharedInstance];
currentContext = [fetcher managedObjectContext];
fetchedResultsController = [fetcher fetchedResultsControllerForEntity:@"Person" withPredicate:nil];
}
return self;
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Contacts";
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Person *person = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [person name];
// Configure the cell...
return cell;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[super dealloc];
}
@end
我有一種感覺,我錯過了一些簡單的東西,或者1個關閉,但我似乎無法找到它。 [FlickrFetcher sharedInstance]返回一個單例,如果我在每個.m文件中拋出幾個斷點,我可以看到* fetcher在每個區域中的對象ID是相同的。
* facedesk * * facedesk * * facedesk *就是這樣,在試圖追蹤一個可能丟失的引用時,我忘記了performFetch。現在它可以工作,非常感謝你。另外,如果有人看到我發佈的任何愚蠢的錯誤,隨時可以對我大吼一聲。 – Chuck 2010-11-30 08:41:44