0

我正在做一个 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

提前致谢

4

2 回答 2

0

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

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

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

希望这可以帮助。

于 2012-06-08T18:01:11.653 回答
0

您是否在界面生成器中设置了 tableview 数据源并委托给您的 nib 文件中的视图?您必须通过设置在 viewdidload 中或在代码中设置它们

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

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

[tableOne removeAllObjects];
于 2012-06-08T10:03:09.533 回答