我建了这样的两个页面,首页(1号页面)有个按钮能点击能连接到另一个页面(2号页面),在2号页面里写入数据,保存在sqlite中 ,我想在首页把存储在sqlite中数据显示出来,怎么就不显示呢 ,像这样的显示数据要注意什么么?我把首页的代码都粘过来。不知道问答这里能不能上传个压缩文件呢?以下是代码,请各位前辈指点。
#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
#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
解决方案 »
- why NSNumber didn't show correct number
- XCode 4.5 在IOS5.1.1真机调试秒退
- IPHONE 4.3后JAVASCRIPT的兼容性问题
- ucenter开发的网站移植到ios平台
- ios中uiimageview中startAnimating有回调函数吗?
- 新手求解
- IOS 文章列表,且点击弹框的怎么搞,求指导~
- iOS 有内存泄露 但是找不到泄露的地方。
- IOS 获取第三方键盘的高度
- 我用NSLog 的那个样式? 各位亲帮个忙。
- 地图位置点的圆圈放大效果是怎样弄上去的?
- 怎么在DetailViewController.m里得到RootViewController.m中的section
[你的tableview对象 reloadData];