BookMessage

2017-12-11  本文已影响0人  Wang99
屏幕快照 2017-12-11 上午8.04.46.png

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *VC = [[ViewController alloc]init];
    VC.title = @"图书列表";
    UINavigationController *Nav = [[UINavigationController alloc]initWithRootViewController:VC];
    self.window.rootViewController = Nav;
    return YES;
}
@end

LoadData.h继承于NSObject

#import <Foundation/Foundation.h>
#import "Model.h"
#import "FMDatabase.h"
@interface LoadData : NSObject
//单列类
+(instancetype)sharlLoadData;
//添加元素
-(void)AddsharlLoadData:(Model *)model;
//查询
-(NSMutableArray *)Marr;
//删除元素
-(void)deleteharlLoadData:(Model *)model;
//修改元素
//-(void)UPsharlLoadData:(Model *)model;
@end

.m

#import "LoadData.h"
static LoadData *ld = nil;
static FMDatabase *fate;
@implementation LoadData
//单例
+(instancetype)sharlLoadData{
    //静态
    static dispatch_once_t oneet;
    dispatch_once(&oneet,^{
        ld = [[LoadData alloc]init];
        [ld initA];
    });
    return ld;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    if (!ld) {
        ld = [super allocWithZone:zone];
    }
    return ld;
}
//浅复制
-(id)copy{
    return self;
}
//深复制
-(id)mutableCopy{
    return self;
}

-(void)initA{
    //创建沙盒
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *path = [str stringByAppendingPathComponent:@"HousingInfo.sqlite"];
    fate = [[FMDatabase alloc]initWithPath:path];
    if ([fate open]) {
        [fate executeUpdate:@"create table class(ID integer primary key,name text,Author text,Price text)"];
        [fate close];
    }
}
//添加元素
-(void)AddsharlLoadData:(Model *)model{
    //开始
    [fate open];
    //初始化
    NSString *str = [NSString stringWithFormat:@"insert into class values (null , '%@','%@','%@')",model.name,model.Author,
                     model.Price];
    //BOOL值接受
    BOOL ii = [fate executeUpdate:str];
    //判断
    if (ii) {
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
    //关闭
    [fate close];
}
//查询
-(NSMutableArray *)Marr{
    //初始化
    NSMutableArray *marr = [NSMutableArray new];
    //开始
    [fate open];
    //初始化
    FMResultSet *Set = [[FMResultSet alloc]init];
    //使用set接受
    Set = [fate executeQuery:@"select * from class"];
    //判断
    while ([Set next]) {
        //初始化
        Model *mm = [Model new];
        //链接
        mm.name = [Set stringForColumn:@"name"];
        mm.Author = [Set stringForColumn:@"Author"];
        mm.Price = [Set stringForColumn:@"Price"];
        mm.ID = [Set intForColumn:@"ID"];
        //添加到数组
        [marr addObject:mm];
        
    }
    //关闭
    [fate close];
    //返回值
    return marr;
}
//删除元素
-(void)deleteharlLoadData:(Model *)model{
    //开始
    [fate open];
    //初始化
    NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
    //BOOL值接受
    BOOL ii = [fate executeUpdate:str];
    //判断
    if (ii) {
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
    //关闭
    [fate close];
}

//修改元素
//-(void)UPsharlLoadData:(Model *)model{
//    //开始
//    [fate open];
//    //初始化
//    NSString *str = [NSString stringWithFormat:@"update class set name = '%@',Author = '%@',Price = '%@', where ID = '%ld'",model.name,model.Author, model.Price,model.ID];
//    //BOOL值接受
//    BOOL ii = [fate executeUpdate:str];
//    //判断
//    if (ii) {
//        NSLog(@"成功");
//    }else{
//        NSLog(@"失败");
//    }
//    //关闭
//    [fate close];
//}
@end

Model.h

#import <Foundation/Foundation.h>

@interface Model : NSObject
@property (nonatomic ,copy)NSString *name;
@property (nonatomic ,copy)NSString *Author;
@property (nonatomic ,copy)NSString *Price;
@property (nonatomic ,assign)NSInteger ID;

@end

AddViewController.m

#import "Model.h"
#import "LoadData.h"
@interface AddViewController ()
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *Author;
@property (strong, nonatomic) IBOutlet UITextField *Price;
- (IBAction)Save:(id)sender;


@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (IBAction)Save:(id)sender {
    //初始化
    Model *mm = [Model new];
    //链接
    mm.name =self.name.text;
    mm.Author =self.Author.text;
    mm.Price =self.Price.text;
    
    
    //添加到数据库
    [[LoadData sharlLoadData]AddsharlLoadData:mm];
    //跳转
    [self.navigationController popViewControllerAnimated:YES];
}
@end

UpViewController.h
``

import <UIKit/UIKit.h>

@class Model;
@interface UpViewController : UIViewController
@property (nonatomic ,strong)Model *mm;

@end

.m

import "UpViewController.h"

import "LoadData.h"

import "Model.h"

@interface UpViewController ()
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *Author;
@property (strong, nonatomic) IBOutlet UITextField *Price;
//- (IBAction)Change:(id)sender;

@end

@implementation UpViewController

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

//- (IBAction)Change:(id)sender {
// //初始化
// Model *mm = self.mm;
// //链接
// mm.name = self.name.text;
// mm.Author = self.Author.text;
// mm.Price = self.Price.text;
//
// //添加
//
// [[LoadData sharlLoadData]UPsharlLoadData:mm];
// //跳转
// [self.navigationController popViewControllerAnimated:YES];
//}
@end

Viewcontroller.m
```#import "ViewController.h"
#import "LoadData.h"//业务处理 SQLite
#import "Model.h" //保存数据

#import "AddViewController.h"
#import "UpViewController.h"
#import "MJRefresh.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MJRefreshBaseViewDelegate>{
    
    NSMutableArray *marr;
}
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) MJRefreshHeaderView *mjHeadView;
@property (nonatomic,strong) MJRefreshFooterView *mjFootView;
@end

@implementation ViewController
//将要显示
-(void)viewWillAppear:(BOOL)animated{
    //查询
    marr = [[LoadData sharlLoadData]Marr];
    //刷新
    [_table reloadData ];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height) style:
              UITableViewStylePlain];
    //添加协议
    _table.delegate =self;
    _table.dataSource =self;
    //添加到试图上
    [self.view addSubview:_table];
    //定义按钮
    UIBarButtonItem *right = [[UIBarButtonItem alloc]
                              initWithTitle:@"+" style:UIBarButtonItemStylePlain
                              target:self action:@selector(click)];
    //添加到导航调试
    self.navigationItem.rightBarButtonItem = right;
    
   
    self.mjHeadView = [[MJRefreshHeaderView alloc]initWithScrollView:self.table];
    
    __weak ViewController *weakSelf = self;
    weakSelf.self.mjHeadView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self getURLDatas];  //请求网络数据
    };
    [self.mjHeadView beginRefreshing];
    
}

//请求网络数据
-(void)getURLDatas{
    //模拟网络延时状态
    dispatch_async(dispatch_get_main_queue(), ^{
        [NSThread sleepForTimeInterval:2.0];
        
    });
    Model *mm = [[Model alloc]init];
    mm.name = @"asd";
    mm.Author = @"sd";
    mm.Price = @"20";
    [[LoadData sharlLoadData]AddsharlLoadData:mm];
    [marr addObject:mm];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.table reloadData];
        if (self.mjHeadView.refreshing) {
            [self.mjHeadView endRefreshing];
        }
    });
}
-(void)click{
    //初始化
    AddViewController *add = [AddViewController new];
    //跳转
    [self.navigationController pushViewController:add animated:YES];
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return marr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"CELLID";
    //初始化
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    //复用池
    if (!cell) {
        //初始化‘
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    //初始化
    Model *mm =marr[indexPath.row];
    //添加到表格上
    cell.textLabel.text = mm.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"作者:%@   价格:%@",mm.Author,mm.Price];
    
    
    //返回值
    return cell;
}
//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //添加
    Model *mm  =marr[ indexPath.row];
    //删除
    [[LoadData sharlLoadData]deleteharlLoadData:mm];
    [marr removeObjectAtIndex:indexPath.row];
    //刷新
    [_table reloadData];
}
//跳转
//-(void)tableView:(UITableView *)tableView
//didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    //初始化
//    UpViewController *up = [UpViewController new];
//    //添加
//    up.mm = marr[indexPath.row];
//    //修改
//    [[LoadData sharlLoadData]UPsharlLoadData:up.mm];
//    //跳转
//    [self.navigationController pushViewController:up animated:YES];
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)Save:(id)sender {
}
@end
上一篇下一篇

猜你喜欢

热点阅读