#import "mygreenteatableVIEwController.h"
@interface mygreenteatableVIEwController ()
@end
@implementation mygreenteatableVIEwController
@synthesize Such;
- (ID)initWithStyle:(UItableVIEwStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(Nsstring*)databasePath //===========我把加载数据和获得路径的过程封装为一个方法
Nsstring *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);
docsDir = [dirPaths objectAtIndex:0];
Nsstring *databasePath = [[Nsstring alloc] initWithString:[docsDir stringByAppendingPathComponent:@"mygreenteainfo.db"]];//新建一个数据的名字
return databasePath; //返回路径
- (voID)vIEwDIDLoad
{
NSLog(@"%@",NSHomeDirectory());
[super vIEwDIDLoad];
Nsstring *databasePath=[self databasePath];//调用我原来封装的方法
NSfileManager *filemanager = [NSfileManager defaultManager];
if ([filemanager fileExistsAtPath:databasePath] == NO) {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath,&db)==sqlITE_OK) //这里的db是之前的添加的 *db
{
char *errmsg;
const char *createsql = "CREATE table IF NOT EXISTS INFO (ID INTEGER PRIMARY KEY autoINCREMENT,name TEXT,ADDRESS TEXT,PICTURE TEXT)";//这里是sql语言的创建列表
if (sqlite3_exec(db,createsql,NulL,&errmsg)!=sqlITE_OK) {
//status.text = @"create table Failed."; 用来提示的lable
NSLog(@"create db ok");
}
}
}
//===============loadDATA==========================================
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
NSMutableArray *such =[[NSMutableArray alloc]initWithCapacity:30];
for (int i=1; i<4; i++) {
if (sqlite3_open(dbpath,&db)==sqlITE_OK) {
Nsstring *querysql = [Nsstring stringWithFormat:@"SELECT name from info where ID=\"%d\"",i]; //================这里就实现了用号码查询,其他的可以另外实现
const char *querystatement = [querysql UTF8String];
if (sqlite3_prepare_v2(db,querystatement,-1,&statement,NulL)==sqlITE_OK) {
if (sqlite3_step(statement)==sqlITE_ROW) {
/*
//做个小测试(原来版本)
Nsstring *classnameFIEld = [[Nsstring alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement,0)];
Nsstring *nameFIEld = [[Nsstring alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,1)];
classname.text = classnameFIEld;
name.text = nameFIEld;
*/
//=======测试部分=====================
Nsstring *tab=[[Nsstring alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement,0)];
// select t.user_ID,random() as Random from udb_user t limit 10;
[such addobject:tab];
self.Such=such;
}
// classname.text=[such objectAtIndex:0];
// name.text=[such objectAtIndex:1];
//=======================================
//status.text = @"find~~~";
sqlite3_finalize(statement);
}
sqlite3_close(db);
//============
- (voID)dIDReceiveMemoryWarning
[super dIDReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw
return 1;
- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section
return Such.count;
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath
static Nsstring *CellIDentifIEr = @"cell";
UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr];
if (cell == nil) {
cell=[[UItableVIEwCell alloc]initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr];//设置风格
[cell setBackgroundcolor:[UIcolor colorWithRed:1 green:1 blue:1 Alpha:0.5]];//这样就可以把cell设成半透明
NSInteger row=[indexPath row];
cell.textLabel.text=[Such objectAtIndex:row];//用数组的数据加载名字!
cell.accessoryType=UItableVIEwCellStyleDefault;//后面那个标标的风格;
return cell;
}
总结以上是内存溢出为你收集整理的如何通过sqlite加载数据到tableview全部内容,希望文章能够帮你解决如何通过sqlite加载数据到tableview所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)