ViewController中的UITableview连接到SQLite

时间:2012-06-08 09:53:11

标签: iphone ios ipad

我正在做一个iPad应用..其中我想从数据库填充UITabelView中的数据,因为它是UIViewController中的tabelview而不是UITableViewController,我不知道如何连接它们。请帮助我。!!! !!

找到我的代码供您参考。

viewController.h:

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



@interface ViewController : UIViewController  {
    NSString *databaseName;
    NSString * databasePath;
    NSMutableArray *tableOne;
    NSString * string1;
    IBOutlet UITableView *tabelView;
}
@property(nonatomic, retain)  NSMutableArray  *tableOne;
@property (strong, nonatomic) IBOutlet UITableView *tabelView;
@property(nonatomic, retain)  NSString * string1;

-(void)checkAndCreateDatabase;
-(void)readDataFromDatabase;
@end


#import "ViewController.h"
#import "db.h"


@implementation ViewController
//inserted 
@synthesize tabelView;
@synthesize tableOne, string1;

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

    databaseName=@"register.db";

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


#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableOne count];
    NSLog(@"a");}
// 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{
    static NSString * CellIdentifier=@"cell";

    UITableViewCell *cell= nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}

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

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;

}


// 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) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    string1 = cell.textLabel.text;
    NSLog(@"%@",string1);
}
-(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;
    tableOne=[[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
        const char *sqlStatement = "SELECT * FROM employee;";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){
            while (sqlite3_step(compiledStatement)==SQLITE_ROW) {
                NSString  *stringName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                db *info =[[db alloc]initWithText:stringName ];
                [tableOne addObject:info];
            }            
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

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


db.h:

#import <Foundation/Foundation.h>

@interface db : NSObject{
    NSString * name;
}

@property (nonatomic, retain) NSString * name;

-(id)initWithText:(NSString *)d ;
@end
#import "db.h"


@implementation db

@synthesize name;

-(id)initWithText:(NSString *)d{
    self=[super init];
    if(self)
    {
        self.name = d;
    }
    return self;
}

@end

先谢谢

2 个答案:

答案 0 :(得分:0)

您是否设置了tableview数据源并委托给界面构建器中的nib文件中的视图?您必须通过设置

在viewdidload中设置那些或代码
self.tabelView.datasource = self;
self.tabelView.delegate = self;

此外,您的tableOne可变数组应该只在viewDidLoad中初始化一次,当您从数据库重新加载数据时,您应该使用以下命令删除所有数组数据:

[tableOne removeAllObjects];

答案 1 :(得分:0)

我认为在从数据库文件中提取数据之前,应确保UITableView正常工作。只需创建一个NSArray作为数据源,并在-ViewDidLoad()中填写一些数据,看看您的UITableView是否可以获取。在您知道它有效之后,您可以从数据库中获取数据并使用数据填充表格视图。

在您的代码中,我没有看到您将视图控制器指定为UITableViewDelegateUITableViewDataSource。我看到你实现了这些方法,但你肯定需要告诉你的类继承UITableViewDelegateUITableViewDataSource

在此之前,在界面构建器中,将uitableview控件拖到视图后,不要忘记右键单击它并将委托和数据源设置为文件的所有者。

希望这有帮助。