我正在尝试在表格视图中显示一些数据,该表格视图是选项卡栏控制器中视图控制器的一部分。目前我正在尝试在 iPhone Simulator 上运行该应用程序。我将 sqlite 数据库复制到以下位置 - /Users/{username}/Library/Application Support/iPhoneSimulator/4.2/Applications/{appid}/Documents
现在我试图在viewWillAppear
我的视图控制器的方法中获取数据。
#import "FugitivesViewController.h"
#import "MyAppDelegate.h"
#import "Fugitive.h"
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSManagedObjectContext *context = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:context];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
NSLog(@"Error while fetching the results");
}
self.items = mutableFetchResults;
[mutableFetchResults release];
[error release];
[request release];
[context release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.items count];
}
- (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];
}
Fugitive *fugitive = [items objectAtIndex:indexPath.row];
cell.textLabel.text = fugitive.name;
return cell;
}
问题是这在视图控制器中什么也没有显示,也没有错误。我已经在标签栏控制器中连接了所需的插座。
检查调试器,可变数组显示 0 个对象。我刚刚开始使用 iOS 开发。有人可以帮我弄清楚这里可能出了什么问题吗?
更新 - 在Yuji的评论的帮助下,我检查了 iPhone Simulator 的 Documents 文件夹中复制的文件。它没有任何数据。这就是视图没有显示数据的原因。因此,问题似乎出在我用来将文件从项目文件夹复制到应用程序的文档文件夹的代码中。
这是怎么回事....
// MyAppDelegate.m
#import "MyAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self createEditableCopyOfDatabaseIfNeeded];
[self.window addSubview:tabcontroller.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)createEditableCopyOfDatabaseIfNeeded {
NSString *defaultDirectory = [[self applicationDocumentsDirectory] absoluteString];
NSString *writableDBPath = [defaultDirectory stringByAppendingPathComponent:@"iBountyHunder1.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:writableDBPath]) {
NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"iBountyHunder" ofType:@"sqlite"];
if (defaultDBPath) {
NSError *error;
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSLog(@"The error is %@", [error localizedDescription]);
}
}
}
}
不明白为什么这不起作用????