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{
    //靜態(tài)
    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{
    //創(chuàng)建沙盒
    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"];
        //添加到數(shù)組
        [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;
    
    
    //添加到數(shù)據(jù)庫
    [[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"http://業(yè)務處理 SQLite
#import "Model.h" //保存數(shù)據(jù)

#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];
    //添加協(xié)議
    _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];  //請求網(wǎng)絡數(shù)據(jù)
    };
    [self.mjHeadView beginRefreshing];
    
}

//請求網(wǎng)絡數(shù)據(jù)
-(void)getURLDatas{
    //模擬網(wǎng)絡延時狀態(tài)
    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];
}
//行數(shù)
-(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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末壹罚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子系宫,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件生兆,死亡現(xiàn)場離奇詭異,居然都是意外死亡膝宁,警方通過查閱死者的電腦和手機鸦难,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門昆汹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人满粗,你說我怎么就攤上這事辈末。” “怎么了映皆?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵挤聘,是天一觀的道長。 經常有香客問我捅彻,道長,這世上最難降的妖魔是什么步淹? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮缭裆,結果婚禮上,老公的妹妹穿的比我還像新娘澈驼。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布挎塌。 她就那樣靜靜地躺著,像睡著了一般榴都。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缭贡,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音阳惹,去河邊找鬼。 笑死莹汤,一個胖子當著我的面吹牛,可吹牛的內容都是我干的纲岭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼止潮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了钞楼?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤询件,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后宛琅,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡嘿辟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了红伦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡色建,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情某残,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布玻墅,位于F島的核電站,受9級特大地震影響澳厢,放射性物質發(fā)生泄漏。R本人自食惡果不足惜剩拢,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望徐伐。 院中可真熱鬧贯钩,春花似錦办素、人聲如沸角雷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽需曾。三九已至吗坚,卻和暖如春胯舷,著一層夾襖步出監(jiān)牢的瞬間刻蚯,已是汗流浹背桑嘶。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工炊汹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留逃顶,地道東北人讨便。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓以政,卻偏偏與公主長得像,于是被迫代替她去往敵國和親盈蛮。 傳聞我的和親對象是個殘疾皇子废菱,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359