タブバーコントローラーのビューコントローラーの一部であるテーブルビューにいくつかのデータを表示しようとしています。現在、iPhoneシミュレーターでアプリを実行しようとしています。sqliteデータベースを次の場所にコピーしました-/Users/ {username} / Library / Application Support / iPhoneSimulator / 4.2 / Applications / {appid} / Documents
viewWillAppear
現在、ViewControllerのメソッドでデータをフェッチしようとしています。
#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;
}
問題は、これがView Controllerに何も表示されず、エラーも表示されないことです。タブバーコントローラーに必要なコンセントを接続しました。
デバッガーをチェックインすると、可変配列は0個のオブジェクトを示します。iOSの開発を始めたばかりです。誰かがここで何がうまくいかないのかを理解するのを手伝ってくれますか?
更新-Yujiのコメントの助けを借りて、私はiPhoneSimulatorの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]);
}
}
}
}
なぜこれが機能しないのかわかりませんか????