iphone把sqlite中的数据显示到页面中?
我建了这样的两个页面,首页(1号页面)有个按钮能点击能连接到另一个页面(2号页面),在2号页面里写入数据,保存在sqlite中 ,我想在首页把存储在sqlite中数据显示出来,怎么就不显示呢 ,像这样的显示数据要注意什么么?我把首页的代码都粘过来。不知道问答这里能不能上传个压缩文件呢?以下是代码,请各位前辈指点。[code=C/C++][/code]
#import "SQL_DemoViewController.h"
#import "User.h"
#import "ResultViewcontroller.h"
@implementation SQL_DemoViewController
@synthesize resultViewController;
@synthesize addNavigationController;
@synthesize dataView;
-(NSString*)databasePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathname = [path objectAtIndex:0];
return [pathname stringByAppendingPathComponent:DataFile];
}
-(id)initWithDelegate:(id)delegate
{
if (self = [super init]) {
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
dataView=[[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 400.0) style:UITableViewStylePlain];
self.dataView.delegate=self;
self.dataView.dataSource=self;
dataView.separatorColor=[UIColor lightGrayColor];
[self.view addSubview:dataView];
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle: @"new" style:UIBarButtonItemStyleBordered target:self action:@selector(addView)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self getToTalCount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"CellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
User *user=[self getUser:[indexPath row]+1];
cell.textLabel.text=[NSString stringWithFormat:@"%@ | %@ ",user.id,user.detail];
return cell;
}
-(void)addView
{
ResultViewcontroller *controller = self.resultViewController;
if (addNavigationController == nil) {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
self.addNavigationController = navController;
[navController release];
}
ResultViewcontroller *resultViewcontroller = [[ResultViewcontroller alloc] initWithDelegate:self];
[self.navigationController pushViewController:resultViewcontroller animated:YES];
}
-(User*)getUser:(int)id
{
if(sqlite3_open([[self databasePath] UTF8String],&database)!=SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0,@"open database faild!");
return nil;
}
User *user=[[User alloc] init];
NSString *countSQl=[NSString stringWithFormat:@"SELECT ROW,DETAIL FROM UsrTable WHERE ROW=%i",id];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database,[countSQl UTF8String],-1,&statement,nil)==SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_ROW)
{
user.id=[NSString stringWithFormat:@"%d",sqlite3_column_int(statement,0)];
user.detail=[NSString stringWithFormat:@"%s",sqlite3_column_text(statement,0)];
}
//sqlite3_finalize(statement);
return user;
}
return nil;
}
-(NSInteger)getToTalCount
{
if(sqlite3_open([[self databasePath] UTF8String],&database)!=SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0,@"open database faild!");
return 0;
}
NSString *countSQL=@"SELECT COUNT(*) FROM UsrTable";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database,[countSQL UTF8String],-1,&statement,nil)==SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_OK)
{
int i=sqlite3_column_int(statement,0);
//sqlite3_finalize(statement);
return i;
}
}
return 0;
}
- (void)dealloc {
[super dealloc];
}
@end
[解决办法]
你这个类中的table要调用reloadData方法,可以再viewWillAppear方法中调用
[你的tableview对象 reloadData];
[解决办法]
调试一把 先看数据能出来不
[解决办法]
打印一下log吧,看看到底有木有得到数据