2011-01-20 28 views
0

タブバーコントローラ内のビューコントローラの一部であるテーブルビューに一部のデータを表示しようとしています。現在のところ、私は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]); 
      } 
     } 
    } 
} 

が取得できません....それがどのようになるで????

+0

どのようにsqliteデータベースを作成しましたか? – Yuji

+0

@Yuji - ヘッドファーストiPhone開発のチュートリアルに従っています - ここからhttp://www.headfirstlabs.com/iphonedevのsqliteデータベースをコピーしました - 第7章 – vikmalhotra

+0

viewWillAppearで管理されたアプリケーションコンテキストを解放することが正しくありません。オブジェクトのポインタが取得されましたが、所有権は取られませんでした。 – Evan

答えて

0

私は第7章のコードサンプルをダウンロードしました。これをXCodeで開き、Project-> Edit Project Settings-> Base SDKを最新のsdk(4.2)に更新しました。新しいプロジェクトを最初から作成する予定だった場合は、プロジェクトがCore Dataによってサポートされ、プロジェクトのコードが正しくコピーされて貼り付けられることを確認してください。

0

あなたのクラスは#import "MyAppDelegate.h"で始まります。これは、ビューコントローラではなく、あなたのアプリケーションデリゲートだと思わせてくれます。それがビューコントローラでない場合、私は- (void)viewWillAppear:(BOOL)animatedが実行されるとは思わない。そのメソッドにNSLog(@"this code is being run");を入れて実行しているかどうか確認してください。

+0

ここに行をコピーできませんでした。 View Controllerのクラスのインポートを編集しました。上のコードはView Controllerのみです。私は 'myAppDelegate.h'をインポートして' managedObjectContext'への参照を取得しています。 – vikmalhotra

1

sqlite3ファイルを自分でDocumentsディレクトリにコピーしましたか? コードは、そこに見つからない場合は、Documentsディレクトリへの書き込み可能なデータベースコピーを作成します。

以下を実行してください。

  1. は、シミュレータ終了iBountyHunter
  2. を削除するiPhoneシミュレータ
  3. にXcodeで、私はちょうど気づいた再び
  4. プロジェクトを実行し
1

  • を押してホームボタンをプロジェクトを実行しますあなたはあなたのdb "iBountyHunder"と "iBountyHunder1"を上のコードスニペットの1つに付けました。 Head Firstが提供するファイルは "iBountyHunter"(t、最後にdではない)です。

    コアデータテンプレートでは、プロジェクト名がdbファイル名と一致すると予想されるため、プロジェクト名がdbファイル名と一致しているかどうかを確認したい場合があります。

    私は、DBの作業を得ていた、私もそれがビルドを使用するたびに役立った - >クリーンすべてのターゲットだけでなく、iPhoneSimulator - > [コンテンツと設定をリセット...

  • 1

    私はときに、同じエラーを得ましたその本のコードに従おうとしています。書き込み可能なパスで利用可能なデータベースをチェックして、.sqliteファイルの内容を確認したところ、 私はデータベースの名前がプロジェクト名と一致するはずです。インポートステートメントを見ると、プロジェクトの名前はMyのようになります。したがって、データベースはファイルの名前としてMy.sqliteでコピーする必要があります。私は自分のMacBookで試してみました。

    参考までに、sqlite database browserを使用して.sqliteファイルの内容を確認しています。

    関連する問題