2012-02-29 2 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 

내가 아이 패드 개발에 새로운 오전 ..

여기 강타 제발 도와주세요
+0

이 세 클래스 간의 관계는 무엇입니까? – Ravin

+0

안녕하세요 Ravin : 하나는 데이터베이스, tableview 및 textview입니다 – makumar

+0

버튼은 어디에 있습니까? –

답변

0

빠르고 간단하게 UITextView를 제거한 다음 행을 선택할 때 다시 추가하는 것입니다. 그게 작동하는지 확인하십시오.

+0

안녕하세요 Hisoka 나에게 약간의 예 또는 어떤 링크를 줘 줄 수 있냐? – makumar

0

나는 이것이 당신이 묻는 질문이 아니라는 것을 알지만, 필요에 따라 핵심 데이터를 대신 사용할 수 있습니다. .sqlite 데이터베이스를 추상화하고 메모리 사용량, 모델링 오브젝트 및 관계 등에 관해서 멋진 것들을 수행합니다.

+0

CoreData를 사용해 보았는데 절대적인 악몽이었습니다. 나는 사람들이 그것을 사용하는 것을 추천 할 수있는 방법을 아직도 얻지 못한다. 대안은 복잡하고 지루한 (수동 쿼리 작성) 것이지만 CoreData를 사용하여 테이블 스키마를 변경해야 할 때 문제가있었습니다. –

+0

우리는 스키마를 항상 변경하고 필드의 기존 데이터를 자동으로 업그레이드합니다. 나는 학습 곡선이 있다는 데 동의하지만, 충분히 발전된 OR 매퍼가 있으면 항상 거기에 있습니다. YMMV - 분명히 당신에게 가장 좋은 것을 사용하십시오. –