在我的应用程序中,我在sqlite表DataBase中存储了2个数组,但我无法在UItableVIEw中从sqlite显示.
我在谷歌搜索但没有找到sqlite故事板中的显示数据.!!!
请指导我如何在tablevIEw中显示sqlite DB中的数据.
这是我的代码:
#import "VIEwController.h"#define Dataname @"DB.sqlite"@implementation VIEwController{ NSDictionary * dictionary;}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; NSArray *name = [NSArray arrayWithObjects:@"Ribery",@"Ronaldo",@"Messi",@"Zannati",@"Totti",nil]; NSArray *Team = [NSArray arrayWithObjects:@"byern",@"R.MadrID",@"barcelona",@"Inter",@"Rome",nil]; for (int i = 0; i < [name count]; i++) { Nsstring * name = [name objectAtIndex:i]; Nsstring * team = [Team objectAtIndex:i]; dictionary = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",team,@"Team",nil]; Nsstring *query = [Nsstring stringWithFormat:@"insert into tableFootball (name,Team) values('%@','%@')",[dictionary objectForKey:@"name"],[dictionary objectForKey:@"Team"]]; [self executequery:query]; }}-(Nsstring *) datafilePath{ NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES); Nsstring *documentsDirectory = [paths objectAtIndex:0]; NSLog(@"PATH %@",[documentsDirectory stringByAppendingPathComponent:Dataname]); return [documentsDirectory stringByAppendingPathComponent:Dataname];}-(voID)executequery:(Nsstring *)query{ //NSLog(@"query : %@",query); sqlite3_stmt *statement; if(sqlite3_open([[self datafilePath] UTF8String],&DataBase) == sqlITE_OK) { if (sqlite3_prepare_v2(DataBase,[query UTF8String],-1,&statement,NulL) == sqlITE_OK) { if (sqlite3_step(statement) != sqlITE_DONE) { sqlite3_finalize(statement); } } else { NSLog(@"query Statement Not Compiled"); } sqlite3_finalize(statement); sqlite3_close(DataBase); } else { NSLog(@"Data not Opened"); }}#pragma mark - table vIEw data source- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{ // Return the number of sections. return 1;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return 1;}- (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]; } //I dont kNow what put here for display from sqlite return cell;}解决方法 创建一个方法,将获取存储在sqlite中的所有玩家,例如findAllPlayers.创建一个dataSourceArray,它将保存从前一个方法返回的玩家.为了便于使用,很容易形成模型自定义对象而不是字典.
//Player.h@interface Player : NSObject@property (nonatomic) NSUInteger playerID;@property (nonatomic,copy) Nsstring *name;@property (nonatomic,copy) Nsstring *team;
实用程序PlayerDatabase,用于从sqlite插入和提取播放器实例.
//PlayerDatabase.h@class Player;@interface PlayersDatabase : NSObject+ (PlayersDatabase*)database;- (NSArray *)findAllPlayers;- (BOol)insertPlayer:(Player *)player;//PlayerDatabase.m@implementation PlayersDatabasestatic PlayersDatabase *_database;+ (PlayersDatabase*)database { if (_database == nil) { _database = [[PlayersDatabase alloc] init]; } return _database;}- (ID)init { if ((self = [super init])) { NSfileManager *fileManager = [NSfileManager defaultManager]; Nsstring *filePath = [self databasePath]; if (![fileManager fileExistsAtPath:filePath]) { Nsstring *sqliteDb = [[NSBundle mainBundle] pathForResource:@"Players" ofType:@"db"]; [fileManager copyItemAtPath:sqliteDb topath:filePath error:NulL]; } } return self;}- (Nsstring *)databasePath{ NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES); Nsstring *documentsDirectory = paths[0]; return [documentsDirectory stringByAppendingPathComponent:@"Players.db"];}- (NSArray *)findAllPlayers{ NSMutableArray *players = [NSMutableArray array]; FMDatabase *database = [FMDatabase databaseWithPath:[self databasePath]]; [database open]; FMResultSet *resultSet = [database executequery:@"SELECT * from Player ORDER BY name"]; while ([resultSet next]) { Player *player = [[Player alloc]init]; player.playerID = [resultSet intForColumn:@"PlayerID"]; player.name = [resultSet stringForColumn:@"name"]; player.team = [resultSet stringForColumn:@"Team"]; [players addobject:player]; } [database close]; return players;}- (BOol)insertPlayer:(Player *)player{ FMDatabase *db = [FMDatabase databaseWithPath:[self databasePath]]; [db open]; BOol success = [db executeUpdate:@"INSERT INTO Player (name,Team) VALUES (?,?);",player.name,player.team,nil]; [db close]; return success;}
现在在带有tableVIEw的vIEwController中,首先填充sqlite,然后重新加载tableVIEw
//YourVIEwController.h - (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; NSArray *names = [NSArray arrayWithObjects:@"Ribery",nil]; NSArray *teams = [NSArray arrayWithObjects:@"byern",nil]; for (int i = 0; i < [name count]; i++) { Player *player = [[Player alloc]init]; player.name = names[i]; player.team = teams[i]; [[PlayerDatabase database] insertPlayer:player]; } self.players = [self findAllPlayers]; [self.tableVIEw reloadData];}#pragma mark - table vIEw data source- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{ // Return the number of sections. return 1;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return [self.players count];}- (UItableVIEwCell*)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static Nsstring *CellIDentifIEr = @"Cell"; UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if (cell == nil) { cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleSubTitle reuseIDentifIEr:CellIDentifIEr]; } //Here the dataSource array is of dictionary objects Player *player = self.players[indexPath.row]; cell.textLabel.text = player.name; cell.detailTextLabel.text = player.team; return cell;}
我按照上面提到的教程制定了一个示例项目,数据被预加载到sqlite中.所以你可能需要调整它以供你使用.
Source Code
总结以上是内存溢出为你收集整理的ios – 如何在Storyboard中的UITableView中显示sqlite中的数据全部内容,希望文章能够帮你解决ios – 如何在Storyboard中的UITableView中显示sqlite中的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)