0

こんにちは

最後の部分で行き詰まっています。助けてください。

これには3 つのビューがあります。1つ目は書籍のリスト( UITableView内)、2 つ目( UITableView内)、最後の1 つはテキスト形式 ( UITextView 内) のコンテンツ用です。

2番目のビューまでは非常にうまく機能しています。問題は、2 番目のビューで任意の章を選択すると、3 番目のビューのテキスト ビューに何も表示されないことです。ただし、出力では一連の番号が表示されます。

データベースからデータを取得し、UITextView に表示するコード。

 NSMutableString *mStr = [NSMutableString stringWithFormat:@"select content from content where name = \"%@\" and chapterno = %@",chapName,chapNo];
 const char *sql =(char *)[mStr UTF8String];

    sqlite3_stmt *sqlStmt;
    if(sqlite3_prepare(dbContent, sql, -1, &sqlStmt, NULL)!=SQLITE_OK)
    {
        NSLog(@"prob with prepare statement: %s",sqlite3_errmsg(dbContent));
    }
    else
    {
        while (sqlite3_step(sqlStmt)==SQLITE_ROW) {
            len = sqlite3_column_bytes(sqlStmt, 0);
            NSData *cData = [[NSData alloc]initWithBytes:sqlite3_column_blob(sqlStmt, 0) length:len];

            NSString *nstr = [NSString stringWithUTF8String:[cData bytes]];
            myTextView.text = nstr;
  }

上記のchapName最初のビューから取得され、chapNoは2 番目のビューで選択された行です。

コンテンツ テーブルは次のようになります。

 id      bookid      name       chapterno          content

  1        1          abc           1            BLOB(size:4217)  
  2        1          abc           2            BLOB(size:3193)
  3        1          abc           3            BLOB(size:3501)

O/p は、このように長い数列で表示されます。

  <0d0a3120 496e2074 68652062 6567696e 6e696e67 20476f64 20637265 61746564 20746865 20686561 76656e20 616e6420 74686520 65617274 682e0d0a...>

ここでは、テキスト ビューで表示する必要があるテキスト コンテンツです。ここで何が欠けていますか?

4

3 に答える 3

0
  NSMutableArray *ar=[[NSMutableArray alloc] init];
 NSString *strQuery=[NSString stringWithFormat:@"select content from content where name = \"%@\" and chapterno = %@",chapName,chapNo];
 const char *sql =[mStr UTF8String];

    sqlite3_stmt *sqlStmt;
    if(sqlite3_open([self.dbPath UTF8String], &amp;database)==SQLITE_OK) 
    {
        if(sqlite3_prepare_v2(database, query, -1, &amp;compiledStmt, NULL)==SQLITE_OK){
         while (sqlite3_step(sqlStmt)==SQLITE_ROW) {
            len = sqlite3_column_bytes(sqlStmt, 0);
            NSData *cData = [[NSData alloc]initWithBytes:sqlite3_column_blob(sqlStmt, 0) length:len];

            NSString *nstr = [NSString stringWithUTF8String:[cData bytes]];
            myTextView.text = nstr;

         [ar addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 1) length:sqlite3_column_bytes(compiledStmt, 1)],@"data1",
                                  [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStmt, 2)],@"data2",nil]];
    }
    else {
                NSLog(@"Invalid query");
            }
 NSArray *arToReturn=([ar count]>;0)?[NSArray arrayWithArray:ar]:nil;
        ar=nil;
return arToReturn;
    }

次のコードを実装して、生成された問題とクエリをお知らせください。

于 2013-05-29T10:15:58.673 に答える
0

そのための最も簡単な代替方法を見つけました。

それ以来、データベースから本の名前(最初のテーブル ビューから) と章番号(2 番目のテーブル ビューから) を取得し、詳細ビュー ページに渡します。

ここで、私の本の名前は数学、物理学、地理学であり、章は数字 (1、2、3...) であるとしましょう。

  [booknamechpatername.txt]
#book name- Maths
  chapter1-->> maths1.txt
  chapter2-->> maths2.txt
  ...
#book name - Physics
  chapter1 -->>  physics12.txt
  ...
  geography21.txt
 ...so on..

それをiPhoneリソースまたは必要なフォルダーにドラッグします。次に、そのファイルを文字列に保存し、UITextView に表示します。

-(void)loadFile{
   NSError *error;
   NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
   NSString *docDir = [dirPath objectAtIndex:0];
   if (!docDir) {
       NSLog(@"Doc dir not found");
    }
   NSString *str = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%@",chapName,chapNo] ofType:@"txt"];

   NSString *content = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:&error];
   myTextView.text = content;
}

そしてそれをviewDidLoadで呼び出します

 [self loadFile];

それ以外の場合、データベースのみを調べたい場合は、このリンクhttps://github.com/jblough/bible-iosが役立ちます。

于 2013-06-05T06:38:24.697 に答える