我正在做一个 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
提前致谢