ios中常用的数据是sqlite,常见的编辑器是lita
新建数据库DishOrder,两张表一张是Category,一张是Dish表。
Category表中,一个是Categoryid,一个是CategoryTitle.
Dish表中,Dishid,DishTitle,DishWeight,DishPrice,Categoryid
把数据库文件加入前一个工程。下载FMDB库。
https://github.com/ccgus/fmdb
删除FMDB.m文件。添加FrameWork
libsqlite3.dylib
TableViewController添加成员变量
NSMutableArray *_items;
修改TableViewController.m
- (void)viewDidLoad
{
[superviewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//self.tableView.rowHeight=80;
self.title=@"首页";
_items=[[NSMutableArrayalloc] init];
FMDatabase *db=[FMDatabasedatabaseWithPath:[[NSBundlemainBundle] pathForResource:@"DishOrder"ofType:@"db"]];
if ([db open]) {
FMResultSet *fResult =[db executeQuery:@"select * from category"];
while ([fResult next]) {
NSString *t=[fResult stringForColumn:@"categoryTitle"];
[_itemsaddObject:t];
}
[fResultclose];
}
[dbclose];
}
- (void) viewDidUnload
{
[_itemsrelease];
_items=nil;
[superviewDidUnload];
}
- (void)dealloc
{
[_itemsrelease];
[superdealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_itemscount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
// Configure the cell...
if (!cell) {
cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];
}
cell.textLabel.text=[_itemsobjectAtIndex:indexPath.row];
return cell;
}
源码地址:http://download.csdn.net/detail/cloud95/5191785