大佬教程收集整理的这篇文章主要介绍了ios – 如何在Storyboard中的UITableView中显示sqlite中的数据,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#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 @R_944_3244@ of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return @R_944_3244@ 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; }
//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 PlayersDatabase static 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 @R_944_3244@ of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return @R_944_3244@ 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; }
以上是大佬教程为你收集整理的ios – 如何在Storyboard中的UITableView中显示sqlite中的数据全部内容,希望文章能够帮你解决ios – 如何在Storyboard中的UITableView中显示sqlite中的数据所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。