iphone把sqlite中的数据显示到页面中?

iOS 码拜 9年前 (2015-05-06) 514次浏览 0个评论
 

我建了这样的两个页面,首页(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

我现在在查是我显示的问题,还是select数据库的问题。
40分
你这个类中的table要调用reloadData方法,可以再viewWillAppear方法中调用
[你的tableview对象 reloadData];
我加上了reloadData 但是问题仍然存在,然后我把-(NSInteger)getToTalCount这里的 return 0去掉后就可以显示存入的数据了,但是又出现了另一个问题。在没有数据的TableView单元一概显示成NULL。怎么解决。
30分
调试一把 先看数据能出来不
30分
打印一下log吧,看看到底有木有得到数据

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明iphone把sqlite中的数据显示到页面中?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!