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;
}
//淺復(fù)制
-(id)copy{
    return self;
}
//深復(fù)制
-(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(@"失敗");
    }
    //關(guān)閉
    [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];
        
    }
    //關(guān)閉
    [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(@"失敗");
    }
    //關(guān)閉
    [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(@"失敗");
//    }
//    //關(guān)閉
//    [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];
    //跳轉(zhuǎn)
    [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];
// //跳轉(zhuǎn)
// [self.navigationController popViewControllerAnimated:YES];
//}
@end

Viewcontroller.m
```#import "ViewController.h"
#import "LoadData.h"http://業(yè)務(wù)處理 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)];
    //添加到導(dǎo)航調(diào)試
    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)絡(luò)數(shù)據(jù)
    };
    [self.mjHeadView beginRefreshing];
    
}

//請求網(wǎng)絡(luò)數(shù)據(jù)
-(void)getURLDatas{
    //模擬網(wǎng)絡(luò)延時(shí)狀態(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];
    //跳轉(zhuǎn)
    [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];
    //復(fù)用池
    if (!cell) {
        //初始化‘
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    //初始化
    Model *mm =marr[indexPath.row];
    //添加到表格上
    cell.textLabel.text = mm.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"作者:%@   價(jià)格:%@",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];
}
//跳轉(zhuǎn)
//-(void)tableView:(UITableView *)tableView
//didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    //初始化
//    UpViewController *up = [UpViewController new];
//    //添加
//    up.mm = marr[indexPath.row];
//    //修改
//    [[LoadData sharlLoadData]UPsharlLoadData:up.mm];
//    //跳轉(zhuǎn)
//    [self.navigationController pushViewController:up animated:YES];
//}



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


- (IBAction)Save:(id)sender {
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容