0

私はiPadアプリをやっています..UITableViewControllerではなくUIViewControllerのtabelviewであるため、データベースからUITabelViewにデータを入力したいのですが、それらを接続する方法がわかりません..親切に助けてください.!!!!!

参照用に私のコードを見つけてください。

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

前もって感謝します

4

2 に答える 2

0

UITableViewデータベースファイルからデータを引き出す前に、作業内容を確認する必要があると思います。単にNSArrayデータソースとして作成し、にいくつかのデータを入力して、取得できる-ViewDidLoad()かどうかを確認UITableViewします。そして、それが機能することを知ったら、データベースからデータを取得し、そのデータを使用してテーブルビューにデータを入力できます。

あなたのコードから、あなたがあなたのViewControllerをUITableViewDelegateととして指定しているのを見ませんでしUITableViewDataSourceた。これらのメソッドを実装したようですが、クラスに継承するように指示する必要があり UITableViewDelegateますUITableViewDataSource

その前でも、Interface Builderで、uitableviewコントロールをビューにドラッグした後、それを右クリックして、デリゲートとデータソースをファイルの所有者に設定することを忘れないでください。

お役に立てれば。

于 2012-06-08T18:01:11.653 に答える
0

インターフェイス ビルダーで nib ファイルのビューに tableview データソースとデリゲートを設定しましたか? それらをそこに、またはコードで設定する必要があります。

self.tabelView.datasource = self;
self.tabelView.delegate = self;

また、tableOne 可変配列は viewDidLoad で 1 回だけ初期化する必要があり、データベースからデータをリロードするときは、次を使用してすべての配列データを削除する必要があります。

[tableOne removeAllObjects];
于 2012-06-08T10:03:09.533 に答える