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

這三個類之間的關係是什麼? – Ravin 2012-02-29 13:05:35

+0

嗨Ravin:一個是數據庫,tableview和textview – makumar 2012-02-29 13:14:29

+0

你的按鈕在哪裏? – 2012-02-29 13:49:48

回答

0

一種快速而骯髒的方式就是移除UITextView,並在選擇行時將其添加回來。看看是否有效。

+0

嗨Hisoka你能給我一些例子或任何鏈接嗎? – makumar 2012-03-01 04:40:32

0

我意識到這不是您問的問題,但根據您的需要,您可能需要使用核心數據。它從你的數據庫中抽象出.sqlite數據庫,並在內存使用,建模對象和關係等方面做了一些很酷的事情。

+0

我嘗試使用CoreData,這是一個絕對的噩夢。我仍然不明白人們如何推薦使用它。另一種方法是混亂和繁瑣(編寫手動查詢),但是當我必須使用CoreData更改表的模式時遇到問題。 – 2012-02-29 14:49:15

+0

我們隨時更改模式,並自動升級現場的現有數據。我同意有一個學習曲線,但總是有足夠先進的OR映射器。 YMMV雖然 - 顯然使用什麼是最適合你的。 – 2012-02-29 20:16:22