BookMessage

屏幕快照 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

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.name.text =self.mm.name;
    self.Author.text =self.mm.Author;
    self.Price.text =self.mm.Price;
    }

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

/*

pragma mark - Navigation

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

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

//- (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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350