2012-02-29 14 views
3

私はiPadアプリをやっていますが、私はUITabelviewとButtonとUiTextviewを同じ画面に表示しますか?私の仕事は、私がUITableviewでいくつかの行を選択し、ボタンを押すと、テキストがUITextviewに表示されなければならないということです。SQlite3をUITextviewに接続する

私はいくつかの方法を記入しましたが、うまくいきませんでしたが、私はこの仕事を成功裏に完了するために何ができるか教えていただけますか?

あなたの参照のために私のコードを見つけてください...私の問題を説明するのを助けるかもしれません。

#import <UIKit/UIKit.h> 
#import "table1.h" 
#import "textView.h" 


@interface searchOne : UIViewController 

{ 
    IBOutlet UITableView *firstTable; 
    table1 *tableOne; 
    textView * text1; 
    int row; 
} 
@property(nonatomic,retain)IBOutlet UIButton * search; 
@property (nonatomic,retain) IBOutlet UITextView *txt; 
@property(nonatomic, assign) int row; 

-(IBAction)resPage:(id)sender; 
@end 
#import "searchOne.h" 
#import "textView.h" 

@implementation searchOne 
@synthesize search; 
@synthesize txt, row; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    if (tableOne == nil) { 
     tableOne = [[table1 alloc] init]; 
    } 

    [firstTable setDataSource:tableOne]; 

    tableOne.view = tableOne.tableView; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    row = [indexPath row]; 


} 
-(IBAction)resPage:(id)sender{ 

    NSString *str = txt.text; 
    row = [str intValue]; 
    NSLog(@"get clicked"); 
    self.view =txt; 

} 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 



- (void)viewDidUnload { 
// [self setTxt:nil]; 
[super viewDidUnload]; 
         } 
@end 

DB:

#import <Foundation/Foundation.h> 

@interface db : NSObject{ 
    NSInteger ID; 
    NSString * name; 
} 


@property (nonatomic, retain) NSString * name; 
@property(nonatomic, readwrite) NSInteger ID; 



-(id)initWithID: (NSInteger)i andName:(NSString *)n; 

@end 
@implementation db 
@synthesize name,ID; 

-(id)initWithID: (NSInteger)i andName:(NSString *)n{ 
    self=[super init]; 
    if(self) 
    { 
     self.ID = i; 
     self.name = n; 
    } 
    return self; 
} 


@end 

Tabelビュー:

#import <UIKit/UIKit.h> 
#import "sqlite3.h" 
#import "db.h" 

@interface table1 : UITableViewController<UITableViewDataSource>{ 
    NSString *databaseName; 
    NSString * databasePath; 
    NSMutableArray * tableOne; 

} 
@property(nonatomic, retain) NSMutableArray * tableOne; 


-(void)checkAndCreateDatabase; 
-(void)readDataFromDatabase; 


@end 

#import "table1.h" 
#import "db.h" 

@implementation table1 
@synthesize tableTwo; 

#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 

    [email protected]"nobel10.db"; 

    NSArray *documentPaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString * documentDir = [documentPaths objectAtIndex:0]; 
    databasePath=[documentDir stringByAppendingPathComponent:databaseName]; 
    [self checkAndCreateDatabase]; 
    [self readDataFromDatabase]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 


#pragma mark - TableView Data Source methods 
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [tableTwo count]; } 

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell= nil; 
    cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"]; 
    if (cell == nil) { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];} 

    db * temp =(db *)[self.tableTwo objectAtIndex:indexPath.row]; 
    cell.textLabel.text=temp.name; 
    return cell; 
} 


-(void)checkAndCreateDatabase{ 
    BOOL success; 
    NSFileManager *fileManager=[NSFileManager defaultManager]; 
    success=[fileManager fileExistsAtPath:databasePath]; 
    if(success) 
     return; 

    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:databaseName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
} 
-(void)readDataFromDatabase{ 
    sqlite3 *database; 
    tableTwo=[[NSMutableArray alloc]init]; 
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){ 
     const char *sqlStatement = "SELECT * FROM country"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){ 
      while (sqlite3_step(compiledStatement)==SQLITE_ROW) { 
       NSInteger pId = sqlite3_column_int(compiledStatement, 0); 
       NSString *stringName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       db *info =[[db alloc]initWithID:pId andName:stringName]; 
       [tableTwo addObject:info]; 

      }    
     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
} 



@end 

UITextView:

#import <UIKit/UIKit.h> 
#import "sqlite3.h" 
#import "db.h" 

@interface textView : UIViewController<UITextViewDelegate>{ 
    NSString *databaseName; 
    NSString * databasePath; 
    NSMutableArray *textOne; 
    NSString *description; 
} 
@property(nonatomic, retain) NSMutableArray *textOne; 
@property (nonatomic, retain) NSString *description; 
-(void)checkAndCreateDatabase; 
-(void)readDataFromDatabase; 
-(id)initWithDescription:(NSString *)d; 
@end 

#import "textView.h" 

@implementation textView 
@synthesize textOne, description;; 
#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 

    [email protected]"nobel10.db"; 

    NSArray *documentPaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString * documentDir = [documentPaths objectAtIndex:0]; 
    databasePath=[documentDir stringByAppendingPathComponent:databaseName]; 
    [self checkAndCreateDatabase]; 
    [self readDataFromDatabase]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 


#pragma mark - TableView Data Source methods 


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 


-(void)checkAndCreateDatabase{ 
    BOOL success; 
    NSFileManager *fileManager=[NSFileManager defaultManager]; 
    success=[fileManager fileExistsAtPath:databasePath]; 
    if(success) 
     return; 

    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:databaseName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
} 
-(void)readDataFromDatabase{ 
    sqlite3 *database; 
    textOne=[[NSMutableArray alloc]init]; 
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){ 
     const char *sqlStatement = "SELECT name,item_country.id,text.item FROM country,item_country,text WHERE country.name ='India' AND country.id = item_country.id AND text.item =item_country.item "; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){ 
      while (sqlite3_step(compiledStatement)==SQLITE_ROW) { 
       NSInteger pId = sqlite3_column_int(compiledStatement, 0); 
       NSString *stringName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       db *info =[[db alloc]initWithID:pId andName:stringName]; 
       [textOne addObject:info]; 

      }    
     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
} 
-(id)initWithDescription:(NSString *)d{ 
self.description = d; 
    return self; 
} 

@end 

私はiPadの開発に新しいですし、ここに打た私を助けてください..

+0

これら3つのクラスの関係は何ですか? – Ravin

+0

こんにちはRavin:1つはデータベース、テーブルビュー、テキストビュー – makumar

+0

あなたのボタンはどこですか? –

答えて

0

単純にUITextViewを削除し、行を選択したときに追加するだけです。それが動作するかどうかを確認します。

+0

こんにちは、私はいくつかの例やリンクをお願いしますか? – makumar

0

これはあなたが尋ねた質問ではありませんが、あなたの必要に応じて、代わりにコアデータを使用することをお勧めします。あなたから.sqliteデータベースを抽象化し、メモリ使用量、モデリングオブジェクトおよび関係などに関してかなりクールなことを行います。

+0

私はCoreDataを使用しようとしましたが、それは絶対的な悪夢でした。私はまだ人々がそれを使用することを勧められる方法を取得しません。代わりに、面倒で退屈な(マニュアルクエリを書く)が、私はCoreDataでテーブルのスキーマを変更する必要があったときに問題があった。 –

+0

スキーマは常に変更され、フィールド内の既存のデータが自動的にアップグレードされます。私は学習曲線があることに同意しますが、十分に進歩したORマッパーがあればいつもあります。しかしYMMV - 明らかにあなたにとって最善のものを使用してください。 –

関連する問題