BookManager(ios)

来源:互联网 发布:淘宝对新店的扶持政策 编辑:程序博客网 时间:2024/05/22 02:04

//需要创建的类:
AddViewController:继承于UIViewController (xib)
UpViewController:继承于UIViewController(xib)
Book:继承于NSObject
FMDataBaseC:继承于NSObject
需要导入的第三方:MJRefresh(刷新):由于需要通过刷新添加数据
这里写图片描述
//最终效果通过表格展示

AppDelegate.h

//默认

AppDelegate.m

//AppDelegate.m#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];    return YES;}

ViewController.h

//默认

ViewController.m

//ViewController.m#import "ViewController.h"#import "FMDataBaseC.h"#import "AddViewController.h"#import "UpViewController.h"#import "MJRefresh/MJRefresh.h"#import "Book.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MJRefreshBaseViewDelegate>{    UITableView *theTableView;    NSMutableArray *arr;    MJRefreshFooterView *foot;    MJRefreshHeaderView *header;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //创建表格    theTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];    [self.view addSubview:theTableView];    theTableView.delegate = self;    theTableView.dataSource = self;    theTableView.separatorColor = [UIColor redColor];    //右按钮    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(add)];    foot = [MJRefreshFooterView footer];    foot.scrollView = theTableView;    foot.delegate = self;    header = [MJRefreshHeaderView header];    header.scrollView = theTableView;    header.delegate = self;    [header beginRefreshing];}-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    arr = [[FMDataBaseC shareData] getAll];    //刷新表格    [theTableView reloadData];}- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{    if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {        //2秒以后调用        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{            // 10     1            Book *book=[[Book alloc]init];            book.nameBook=@"秦时明月";            book.name=@"温世仁";            book.price=@"50";            [[FMDataBaseC shareData] insertData:book];           arr = [[FMDataBaseC shareData] getAll];            //刷新表格            [theTableView reloadData];            [header endRefreshing];        });    }    else{        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{            [arr removeObjectAtIndex:0];            [theTableView reloadData];            [foot endRefreshing];        });    }}//添加-(void)add{    AddViewController *add = [[AddViewController alloc] init];    [self.navigationController pushViewController:add animated:YES];}//表格行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return arr.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];    }    Book *boo = arr[indexPath.row];    cell.textLabel.text = [NSString stringWithFormat:@"书名:%@\n",boo.nameBook];    cell.detailTextLabel.text=[NSString stringWithFormat:@"作者:%@\t\t价格:%@\n",boo.name,boo.price];   // cell.textLabel.numberOfLines = 0;    return cell;}//表格行高-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 44;}//修改-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    UpViewController *up = [[UpViewController alloc] init];    up.boo = arr[indexPath.row];    [self.navigationController pushViewController:up animated:YES];}//删除-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    Book *boo = arr[indexPath.row];    [[FMDataBaseC shareData] deleteData:boo];    [arr removeObjectAtIndex:indexPath.row];    [theTableView reloadData];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

AddViewController.h

//默认

AddViewController.m

#import "AddViewController.h"#import "FMDataBaseC.h"@interface AddViewController ()@property (strong, nonatomic) IBOutlet UITextField *nameBookTF;@property (strong, nonatomic) IBOutlet UITextField *nameTF;@property (strong, nonatomic) IBOutlet UITextField *priceTF;@end@implementation AddViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}//添加- (IBAction)addBtn:(id)sender{    Book *boo = [[Book alloc] init];    boo.nameBook = self.nameBookTF.text;    boo.name = self.nameTF.text;    boo.price = self.priceTF.text;    [[FMDataBaseC shareData] insertData:boo];    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"保存成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];    [alert show];    [self.navigationController popViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

AddViewController.xib

这里写图片描述

UpViewController.h

//UpViewController.h#import <UIKit/UIKit.h>#import "Book.h"@interface UpViewController : UIViewController@property(nonatomic , strong)Book *boo;@end

UpViewController.m

#import "UpViewController.h"#import "FMDataBaseC.h"@interface UpViewController ()@property (strong, nonatomic) IBOutlet UITextField *nameBookTF;@property (strong, nonatomic) IBOutlet UITextField *nameTF;@property (strong, nonatomic) IBOutlet UITextField *priceTF;@end@implementation UpViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    self.nameBookTF.text = self.boo.nameBook;    self.nameTF.text = self.boo.name;    self.priceTF.text = self.boo.price;}//修改- (IBAction)upBtn:(id)sender {    Book *boo = [[Book alloc] init];    boo.ids = self.boo.ids;    boo.nameBook = self.nameBookTF.text;    boo.name = self.nameTF.text;    boo.price = self.priceTF.text;    [[FMDataBaseC shareData] upData:boo];    [self.navigationController popViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

UpViewController.xib

将AddViewController.xib中添加改为修改

Book.h

//Book.h@interface Book : NSObject@property(nonatomic , assign)NSInteger ids;@property(nonatomic , strong)NSString *nameBook,*name,*price;@end

Book.m

//默认

FMDataBaseC.h

//FMDataBaseC.h#import <Foundation/Foundation.h>#import "Book.h"#import <FMDatabase.h>#import <FMResultSet.h>@interface FMDataBaseC : NSObject//单例+(instancetype)shareData;//添加-(void)insertData:(Book *)book;//删除-(void)deleteData:(Book *)book;//修改-(void)upData:(Book *)book;//查询-(NSMutableArray *)getAll;@end

FMDataBaseC.m

#import "FMDataBaseC.h"static FMDataBaseC *fm = nil;static FMDatabase *fmdb;@implementation FMDataBaseC//单例+(instancetype)shareData{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        fm = [[FMDataBaseC alloc] init];        [fm initCre];    });    return fm;}+(instancetype)allocWithZone:(struct _NSZone *)zone{    if (!fm) {        fm = [super allocWithZone:zone];    }    return fm;}-(id)copy{    return self;}-(id)mutableCopy{    return self;}//创建数据库-(void)initCre{    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString *path = [str stringByAppendingPathComponent:@"BookManagerSo.DB"];    fmdb = [[FMDatabase alloc] initWithPath:path];    if ([fmdb open]) {        [fmdb executeUpdate:@"create table BookInfoa(ids integer primary key autoincrement, nameBook text, name text, price text)"];        NSLog(@"创建成功");        [fmdb close];    }else{        NSLog(@"创建失败");    }}//添加-(void)insertData:(Book *)book{    [fmdb open];    BOOL insert = [fmdb executeUpdate:@"insert into BookInfoa values(null,?,?,?)",book.nameBook,book.name,book.price];    if (insert) {        NSLog(@"添加成功");    }else{        NSLog(@"添加失败");    }    [fmdb close];}//删除-(void)deleteData:(Book *)book{    [fmdb open];    NSString *str = [NSString stringWithFormat:@"delete from BookInfoa where ids = %ld",book.ids];    BOOL delete = [fmdb executeUpdate:str];    if (delete) {        NSLog(@"删除成功");    }else{        NSLog(@"删除失败");    }    [fmdb close];}//修改-(void)upData:(Book *)book{    [fmdb open];    NSString *str = [NSString stringWithFormat:@"update BookInfoa set nameBook = '%@', name = '%@',  price = '%@' where ids = %ld",book.nameBook,book.name,book.price,book.ids];    BOOL delete = [fmdb executeUpdate:str];    if (delete) {        NSLog(@"修改成功");    }else{        NSLog(@"修改失败");    }    [fmdb close];}//查询-(NSMutableArray *)getAll{    [fmdb open];    NSMutableArray *arr = [NSMutableArray array];    FMResultSet *fmset = [[FMResultSet alloc] init];    fmset = [fmdb executeQuery:@"select * from BookInfoa"];    while ([fmset next]) {        NSInteger ids = [fmset intForColumn:@"ids"];        NSString *nameBook = [fmset stringForColumn:@"nameBook"];        NSString *name = [fmset stringForColumn:@"name"];        NSString *price = [fmset stringForColumn:@"price"];        Book *boo = [[Book alloc] init];        boo.ids = ids;        boo.nameBook = nameBook;        boo.name = name;        boo.price = price;        [arr addObject:boo];    }    [fmdb close];    return arr;}
原创粉丝点击