美文网首页
yymodel 解析 fmdb

yymodel 解析 fmdb

作者: 你又脸红了i | 来源:发表于2019-01-16 09:43 被阅读0次

    导入第三方 AFN yymodel FMDB

    屏幕快照 2019-01-16 上午9.30.53.png
    #import <YYModel.h>
    NS_ASSUME_NONNULL_BEGIN
    //@class ResultModel;
    //@class SmallModel;
    
    
    @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 Model : 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 ZhouKaoModel
    + (NSDictionary *)modelContainerPropertyGenericClass {
        return @{@"result":[ResultModel class]};
    }
    
    DataManager.h
    +(DataManager*)sharedDataHandle;
    
    //增
    
    -(void)addOneMovie:(id)movie;
    
    //删
    -(void)deleteMovieByID:(int)title;
    
    //查
    
    -(id)getAllMovies;
    DataManager.m
    #import "FMDB.h"
    #import "Model.h"
    
    static DataManager*_defaulthandle =nil;
    
    @interface DataManager ()
    
    @property(nonatomic,strong)FMDatabase*fMDB;  //
    
    @end
    
    @implementation DataManager
    +(DataManager*)sharedDataHandle
    
    {
        
        if (_defaulthandle == nil) {
            
            _defaulthandle = [[DataManager alloc] init];
            
            
        }
        
        return _defaulthandle;
        
    }
    
    +(instancetype)allocWithZone:(struct _NSZone*)zone
    
    {
        
        if (_defaulthandle == nil) {
            
            _defaulthandle= [super allocWithZone:zone];
            
        }
        
        return _defaulthandle;
        
    }
    
    -(FMDatabase *)fMDB
    
    {
        
        if(!_fMDB) {
            
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"movies.sqlite"];
            
            NSLog(@"path=====%@",path);
            
            _fMDB= [FMDatabase databaseWithPath:path];
            
            [self initTable];
            
        }
        
        return _fMDB;
        
    }
    
    // 初始化数据表
    
    -(void)initTable
    
    {
        
        [_fMDB open];
        
        [_fMDB executeUpdate:@"CREATE TABLE movie (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,author_name TEXT, url,TEXT)"];
        
        [_fMDB close];
        
    }
    
    -(void)addOneMovie:(SmallModel*)movie
    
    {
        [self.fMDB open];
        
        [self.fMDB executeUpdateWithFormat:@"insert into movie (title,author_name,url) values (%@,%@,%@)",movie.title,movie.author_name,movie.url];
        NSLog(@"插入成功");
        
        [self.fMDB close];
        
    }
    
    -(void)deleteMovieByID:(int)title
    
    {
        
        [self.fMDB open];
        
        [self.fMDB executeUpdateWithFormat:@"DELETE FROM movie WHERE id = %d",title];
        
        [self.fMDB close];
        
    }
    
    -(NSArray*)getAllMovies
    
    {
        
        [self.fMDB open];
        
        
        
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        
        FMResultSet *result = [self.fMDB executeQuery:@"SELECT * FROM movie"];
        
        while([result next])
        {
            SmallModel *movie = [[SmallModel alloc]init];
            
            [arr addObject:movie];
            
            movie.ID= [result intForColumnIndex:0];
            
            movie.title= [result stringForColumnIndex:1];
            
            movie.author_name = [result stringForColumnIndex:2];
            
            movie.url = [result stringForColumnIndex:3];
        }
        
        [self.fMDB close];
        
        return[arr copy];
        
    }
    
    viewController.m
    #import "AFNetworking.h"
    #import "Model.h"
    #import "VcTableViewCell.h"
    #import "TwoViewController.h"
    #import "RightViewController.h"
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
        Model *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
        [manager POST:@"http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb&type=top" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSLog(@"%@",responseObject);
    
            self->mod = [Model yy_modelWithJSON:responseObject];
            [self.array addObject:self->mod.result.data];
            self.array = self->mod.result.data;
            //刷新表格
            [self.ojtable reloadData];
        } 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;
        
        
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        TwoViewController *two = [TwoViewController new];
        SmallModel *model = self.array[indexPath.row];
        two.StrUrl = model;
        [self.navigationController pushViewController:two animated:YES];
    }
    - (void)right{
        RightViewController *right = [RightViewController new];
        [self.navigationController pushViewController:right animated:YES];
    }
    @end
    
    twoviewcontroller.h
    #import "Model.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface TwoViewController : UIViewController
    @property(nonatomic , strong)SmallModel *StrUrl;
    
    twocontroller.m
    
    #import "DataManager.h"
    @interface TwoViewController ()
    
    @end
    
    @implementation TwoViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(right)];
        
        // web 详情y界面
        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        //转换url
        NSURL *url = [NSURL URLWithString:self.StrUrl.url];
        
        NSURLRequest *requst = [NSURLRequest requestWithURL:url];
        
        // 添加
        [web loadRequest:requst];
        [self.view addSubview:web];
    
    }
    // 点击单行右侧按钮收藏
    - (void)right{
      [[DataManager sharedDataHandle] addOneMovie:self.StrUrl];
        
    }
    
    @end
    
    rightviewcontroller.m
    #import "RXqViewController.h"
    #import "DataManager.h"
    #import "Model.h"
    #import "TwoViewController.h"
    @interface RightViewController ()<UITableViewDelegate,UITableViewDataSource>
    @property(nonatomic , strong)UITableView *ojtable;
    @property(nonatomic , strong)NSMutableArray *array;
    
    @end
    
    @implementation RightViewController
    - (void)viewDidAppear:(BOOL)animated{
        self.array = [[DataManager sharedDataHandle]getAllMovies];
       
        [self.ojtable reloadData];
    }
    - (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.view addSubview:self.ojtable];
    }
    - (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;
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        RXqViewController *rxq = [RXqViewController new];
        SmallModel *model =self.array[indexPath.row];
        rxq.StrUrl = model;
        [self.navigationController pushViewController:rxq animated:YES];
    }
    
    // 左滑删除
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        SmallModel *model = self.array[indexPath.row];
        [[DataManager sharedDataHandle]deleteMovieByID:model.ID];
        self.array = [[DataManager sharedDataHandle] getAllMovies];
        [self.ojtable reloadData];
    }
    @end
    
    
    rxqviewcontroller.h
    #import "Model.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface RXqViewController : UIViewController
    @property(nonatomic , strong)SmallModel *StrUrl;
    @end
    
    rxqviewcontroller.m
    
    // web 详情y界面
        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        //转换url
        NSURL *url = [NSURL URLWithString:self.StrUrl.url];
        
        NSURLRequest *requst = [NSURLRequest requestWithURL:url];
        
        // 添加
        [web loadRequest:requst];
        [self.view addSubview:web];
    

    相关文章

      网友评论

          本文标题:yymodel 解析 fmdb

          本文链接:https://www.haomeiwen.com/subject/fzypdqtx.html