タブバーコントローラ内のビューコントローラの一部であるテーブルビューに一部のデータを表示しようとしています。現在のところ、私はiPhone Simulatorでこのアプリケーションを実行しています。私は、次の場所にsqliteのデータベースをコピーした - /ユーザ/ {ユーザー名} /ライブラリ/ Application Support/iPhoneSimulator/4.2 /アプリケーション/ {APPID} /書類コアデータ:sqliteデータベースからデータを取得する際の問題
を今私は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]);
}
}
}
}
が取得できません....それがどのようになるで????
どのようにsqliteデータベースを作成しましたか? – Yuji
@Yuji - ヘッドファーストiPhone開発のチュートリアルに従っています - ここからhttp://www.headfirstlabs.com/iphonedevのsqliteデータベースをコピーしました - 第7章 – vikmalhotra
viewWillAppearで管理されたアプリケーションコンテキストを解放することが正しくありません。オブジェクトのポインタが取得されましたが、所有権は取られませんでした。 – Evan