YYmodel解析 FMDB收藏

导入 AFN YYmodel 手动FMDB 开网

YYmodel解析 FMDB收藏_第1张图片
image
model.h
#import 
 
NS_ASSUME_NONNULL_BEGIN


//第三层

@interface SmallModel :NSObject
//主见id
@property(nonatomic , assign)int ID;
@property(nonatomic , strong)NSString *title;
//@property(nonatomic , strong)NSString *date;
@property(nonatomic , strong)NSString *url;
@end

//第二层
@interface ResultModel :NSObject
@property(nonatomic , strong)NSString *stat;
@property(nonatomic , strong)NSArray *data;
@end

//第一层
@interface Jiexi : NSObject
@property(nonatomic , strong)NSString *reason;
@property(nonatomic , strong)ResultModel *result;
@end

model.m

@implementation SmallModel

//+ (NSDictionary *)modelCustomPropertyMapper {

//

//    return@{@"cataID" :@"id"};

//

//}

@end

@implementation ResultModel

+ (NSDictionary*)modelContainerPropertyGenericClass {

    return @{@"data":[SmallModel class]};

}

@end

@implementation Jiexi

+ (NSDictionary*)modelContainerPropertyGenericClass {
    return @{@"result":[ResultModel class]};
}

FMDBmodel.h

#import "Jiexi.h"

@interfaceFmdbmodel :NSObject

// 单利方法
+(instancetype)initData;

// 初始化数据库
-(void)initSql;

// 初始化表格
-(void)initTable;

// 添加
-(void)addData:(Jiexi*)data;

// 删除数据
-(void)deletData:(NSInteger)theid;

// 查询数据
-(NSMutableArray*)inquireArr;

// 关闭数据库
-(void)closeSql;

FMDBmodel.m

#import"FMDB.h"

#import "Jiexi.h"

// 创建静态变量
static Fmdbmodel *sql;
static FMDatabase *db;

@implementation Fmdbmodel

// 初始化单利方法
+(instancetype)initData{
    if(!sql){
        sql= [[Fmdbmodelalloc]init];
    }
    return sql;
}

// 初始化数据库
-(void)initSql{
    // 获取Documents目录
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    // 拼接路径
    NSString *fileName = [str stringByAppendingString:@"xinjiexi.db"];
    NSLog(@"file:%@",fileName);
    // 创建数据库
    db = [[FMDatabase alloc]initWithPath:fileName];
    if([db open]){

        NSLog(@"打开数据库成功");
        // 初始化表格
        [self initTable];
    }else{
        NSLog(@"打开数据库失败");
    }
}

// 初始化表格
-(void)initTable{
    //初始化数据库表格的格式:create table if not exists 表名(主键id integer primary key,所有的数据类型);
    [db executeUpdate:@"create table if not exists Jiexi(ID integer primary key,title text,url text)"];
    // 关闭数据库
    [db close];
}

// 添加数据
-(void)addData:(SmallModel*)data{
    if([db open]) {
        //添加数据的sql语句:insert into 表名 values(null,?,?);
        [db executeUpdate:[NSString stringWithFormat:@"insert into Jiexi  values(null,'%@','%@')",data.title,data.url]];
        NSLog(@"插入成功");
        //        NSLog(@"---------%@",data);
    }else{
        NSLog(@"添加失败");
    }

    // 关闭数据库
    [db close];
}

// 删除数据
-(void)deletData:(NSInteger)theid{
    if([db open]) {
        //sql 语句: delete from 表名 where 表名的主键id = ?
        [db executeUpdate:[NSString stringWithFormat:@"delete from Jiexi where ID = %ld",theid]];
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    // 关闭数据库
    [db close];

}

// 查询数据库
-(NSMutableArray*)inquireArr{
    // 创建数据
    NSMutableArray *arr = [NSMutableArray new];
    // 集合
    FMResultSet *set = [FMResultSet new];
    if([db open]) {
        //sql 语句格式:select *from 表名
        set = [db executeQuery:@"select *from Jiexi"];
        // 判断有没有东西
        while([set next]) {
            SmallModel*model = [[SmallModel alloc]init];
            //model.imgsrc = [set stringForColumn:@"imgsrc"];
            model.title= [setstringForColumn:@"title"];
            model.url= [setstringForColumn:@"url"];
            model.ID= [setintForColumn:@"ID"];
            [arr addObject:model];
        }
    }else{
        NSLog(@"查询失败");
    }
    [db close];
    returnarr;

}

ViewController.m

#import "Jiexi.h"

#import "AFNetworking.h"

#import "XiangQingViewController.h"

#import "ShouCangViewController.h"

@interface ViewController (){

    Jiexi*mod;

}

@property(nonatomic , strong)UITableView *ojtable;

@property(nonatomic , strong)NSMutableArray *array;

@end

@implementation ViewController

-(UITableView*)ojtable{

    if(!_ojtable) {

        _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        _ojtable.delegate=self;

        _ojtable.dataSource=self;

    }

    return _ojtable;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收cang" style:UIBarButtonItemStyleDone target:self action:@selector(right)];

    self.array = [NSMutableArray array];

    [self.view addSubview:self.ojtable];

    [self loadData];

}

- (void)loadData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //NSDictionary *dict = @{@"key":@"67968aeebf1f4e3b6170f7217b6f3cdb"};

    // POST请求http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb

    [managerPOST:@"http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb&type=top" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {

    }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"%@",responseObject);

        self->mod= [Jiexiyy_modelWithJSON:responseObject];

        [self.array addObject:self->mod.result.data];

        self.array = self->mod.result.data;

        //刷新表格

        [self.ojtablereloadData];

    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"%@",error);

    }];

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }

    SmallModel*mod =self.array[indexPath.row];

    cell.textLabel.text= mod.title;

    //cell.detailTextLabel.text = mod.author_name;

    returncell;

}

//点击cell跳详情

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    XiangQingViewController *two = [XiangQingViewController new];

    SmallModel*model =self.array[indexPath.row];

    two.StrUrl= model;

    [self.navigationController pushViewController:two animated:YES];

}

//点击按钮跳收藏

- (void)right{

    ShouCangViewController *right = [ShouCangViewController new];

    [self.navigationController pushViewController:right animated:YES];

}

XiangQingViewController.h

@property(nonatomic , strong)SmallModel *StrUrl;

XiangQingViewController.m

#import "Jiexi.h"
#import 
#import "Fmdbmodel.h"

@interface XiangQingViewController ()

@end

@implementationXiangQingViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(collect)];

    //网页视图 ios12摒弃了uiwebview

    WKWebView *webview=[[WKWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];

    //将urlString转换为url

    NSURL *url=[NSURL URLWithString:self.StrUrl.url];

    //创建请求对象

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //网页视图加载网页

    [webviewloadRequest:request];

    //添加到当前视图上

    [self.viewaddSubview:webview];

}

-(void)collect{

    [[Fmdbmodel initData]initSql];

    // NSLog(@"=======%@",self.model);

    [[Fmdbmodel initData]addData:self.StrUrl];

}

@end

ShouCangViewController.m

#import "ShouCangXiangQingViewController.h"

#import "Fmdbmodel.h"

@interface ShouCangViewController ()

@property(nonatomic , strong)UITableView *tbv;

@property(nonatomic , strong)NSMutableArray *arr;

@end

@implementationShouCangViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title=@"收藏列表";

    self.tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tbv.delegate=self;

    self.tbv.dataSource =self;

    [self.view addSubview:self.tbv];

    //[self.tbv registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

}

//视图展示的时候 查询数据库 有没有信息  有展示出来

-(void)viewWillAppear:(BOOL)animated{

    [[Fmdbmodel initData]initSql];

    self.arr = [[Fmdbmodel initData]inquireArr];

    for(SmallModel *dd in self.arr) {

        //NSLog(@"qqqq====%ld",dd.ID);

    }

    [self.tbv reloadData];

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arr.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

//    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//

//    [cell loadDataModel:self.arr[indexPath.row]];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }

    SmallModel*mod =self.arr[indexPath.row];
    cell.textLabel.text= mod.title;
    returncell;

}

// 删除

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

[[Fmdbmodel initData]initSql];

    //NSLog(@"zzzz======%ld",[self.arr[indexPath.row]ID]);
    [[Fmdbmodel initData]deletData:[self.arr[indexPath.row]ID]];

    //删除数据
    [self.arrremoveObject:self.arr[indexPath.row]];
    //[self.array removeAllObjects];
    [self.tbv reloadData];

    //    DataModel *mo = self.arr[indexPath.row];
    //    [[SqliteModel initData]deletData:mo.classID];
    //    self.arr = [[SqliteModel initData]inquireArr];
    //    [self.tbv reloadData];

}

//点击跳转
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    [self.tbv deselectRowAtIndexPath:indexPath animated:YES];

    ShouCangXiangQingViewController *collect = [ShouCangXiangQingViewController new];
    SmallModel*data =self.arr[indexPath.row];
    collect.model= data;
    [self.navigationController pushViewController:collect animated:YES];

}

详情页面代码都一样

你可能感兴趣的:(YYmodel解析 FMDB收藏)