//ViewController代码(model数据以及FMDB包没有上传)
#import "ViewController.h"
#import "Model.h"
#import "FMDBdatabase.h"
#import "AddViewController.h"
#import "XiuGaiViewController.h"
@interface ViewController (){
NSMutableArray *Marr;
}
@property(nonatomic,strong)UITableView *table;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
Marr = [[FMDBdatabase danli]Marr];
[self.table reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.table];
UIBarButtonItem *butt =[[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStylePlain target:self action:@selector(Add)];
self.navigationItem.rightBarButtonItem = butt;
}
-(void)Add{
AddViewController *aa =[[AddViewController alloc]init];
[self.navigationController pushViewController:aa animated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return Marr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
Model *mm =[Marr objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@",mm.name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",mm.age];
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
Model *mm = [Marr objectAtIndex:indexPath.row];
[[FMDBdatabase danli]deletedata:mm];
[Marr removeObjectAtIndex:indexPath.row];
[self.table reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
XiuGaiViewController * up = [[XiuGaiViewController alloc]init];
up.mm = Marr[indexPath.row];
[self.navigationController pushViewController:up animated:YES];
}
-(UITableView *)table{
if (!_table)
{_table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
}
return _table;
}
//数据添加
Model *mm =[[Model alloc]init];
mm.name = self.nameText.text;
mm.age = self.ageText.text;
[[FMDBdatabase danli]inserdata:mm];
//修改界面的代码实现修改功能
在.h中定义
@property(nonatomic,strong)Model *mm;
//在.m 中调用代码
- (void)viewDidLoad {
[super viewDidLoad];
self.xiutext.text = self.mm.name;
self.xiuage.text = self.mm.age;
}
- (IBAction)button:(id)sender {
Model *mm =self.mm;
mm.name = self.xiutext.text;
mm.age = self.xiuage.text;
[[FMDBdatabase danli]updata:mm];
}