FMDB+收藏(无图)

导入Afnetworking , FMDB

//
// ViewController.m
// FMDB+收藏
//
// Created by 哈哈哈 on 2018/8/9.
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import “ViewController.h”
#import “AFNetworking/AFNetworking.h”
#import “Model.h”
#import “RightViewController.h”
#import “TwoRightViewController.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{

}
@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];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“收藏” style:UIBarButtonItemStyleDone target:self action:@selector(right)];
    self.array = [NSMutableArray array];
    [self.view addSubview:self.ojtable];
    [self loadData];

}

  • (void)loadData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:@“http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb” parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingAllowFragments) error:nil];
      
      
      for (NSDictionary *dict in json[@"result"][@"data"]) {
          Model *MT=[Model new];
          [MT setValuesForKeysWithDictionary:dict];
          [self.array addObject:MT];
         
      }
      //刷新表格
      [self.ojtable reloadData];
    

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull 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”];
    }
    Model *MS = self.array[indexPath.row];
    cell.textLabel.text=MS.title;
    cell.detailTextLabel.text = MS.author_name;

    return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RightViewController *r=[RightViewController new];
    Model *MW=self.array[indexPath.row];
    r.urlString=MW;
    [self.navigationController pushViewController:r animated:YES];
    }
    -(void)right{
    TwoRightViewController *two=[TwoRightViewController new];
    [self.navigationController pushViewController:two animated:YES];
    }
    @end
    //
    // RightViewController.h
    // FMDB+收藏
    //
    // Created by 哈哈哈 on 2019/1/9.
    // Copyright © 2019年 哈哈哈. All rights reserved.
    //

#import <UIKit/UIKit.h>
#import “Model.h”
NS_ASSUME_NONNULL_BEGIN

@interface RightViewController : UIViewController
@property(nonatomic,strong)Model *urlString;
@end

NS_ASSUME_NONNULL_END
//
// RightViewController.m
// FMDB+收藏
//
// Created by哈哈哈 on 2018/8/9
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import “RightViewController.h”
#import “SqliteModel.h”
@interface RightViewController (){
UIWebView *webView;
}

@end

@implementation RightViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“收藏” style:UIBarButtonItemStyleDone target:self action:@selector(right)];
    webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString.url]];
    [self.view addSubview: webView];
    [webView loadRequest:request];
    }
    -(void)right{
    //从单立中打开数据库
    [[SqliteModel initData]initSql];
    //将添加的数据给 单利
    [[SqliteModel initData]addData:self.urlString];
    }

@end
//
// TwoRightViewController.h
// FMDB+收藏
//
// Created by 哈哈哈on 2018/8/9.
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface TwoRightViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
//
// TwoRightViewController.m
// FMDB+收藏
//
// Created by哈哈哈 on 2018/8/9
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import “TwoRightViewController.h”
#import “SqliteModel.h”
#import “RightViewController.h”
@interface TwoRightViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *array;

@end

@implementation TwoRightViewController
-(void)viewDidAppear:(BOOL)animated{
//打开数据库
[[SqliteModel initData]initSql];
self.array=[[SqliteModel initData]getDataArray];
NSLog(@“self.array-----===========%@”,self.array);
[self.ojtable reloadData];
}

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.ojtable];
    }
  • (UITableView *)ojtable{
    if (!_ojtable) {
    _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _ojtable.delegate = self;
    _ojtable.dataSource = self;
    }
    return _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”];
    }
    Model *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{
    RightViewController *rxq = [RightViewController new];
    Model *Mmo =self.array[indexPath.row];
    rxq.urlString = Mmo;
    [self.navigationController pushViewController:rxq animated:YES];
    }
    //左滑删除
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //初始化model
    Model *mMD=self.array[indexPath.row];
    [[SqliteModel initData]initSql];
    //删除id
    [[SqliteModel initData]deleteData:mMD.ID];
    // NSLog(@"%@",mMD.ID);
    self.array=[[SqliteModel initData]getDataArray];
    [tableView reloadData];
    }

@end
//
// Model.h
// FMDB+收藏
//
// Created by 哈哈哈 on 2018/8/9.
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject
@property(nonatomic , assign)NSInteger ID;
@property(nonatomic , copy)NSString *title;
@property(nonatomic , copy)NSString *author_name;
@property(nonatomic , copy)NSString *url;
@end

NS_ASSUME_NONNULL_END
//
// Model.m
// FMDB+收藏
//
// Created by 哈哈哈 on 2018/8/9
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import “Model.h”

@implementation Model
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}
@end
//
// SqliteModel.h
// FMDB+收藏
//
// Created by 哈哈哈 on 2018/8/9
// Copyright © 2018年 哈哈哈. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “Model.h”
NS_ASSUME_NONNULL_BEGIN

@interface SqliteModel : NSObject
//单利
+(instancetype)initData;
//初始化数据库
-(void)initSql;
//初始化表格
-(void)initTable;
//增加数据
-(void)addData:(id)data;
//删除
-(void)deleteData:(NSInteger)theid;
//查询
-(NSMutableArray *)getDataArray;
@end

NS_ASSUME_NONNULL_END
//
// SqliteModel.m
// FMDB+收藏
//
// Created by 哈哈哈 on 2018/8/9
// Copyright © 2018年 哈哈哈All rights reserved.
//

#import “SqliteModel.h”
#import “FMDB/FMDB.h”
#import “Model.h”
//静态
static SqliteModel *sql;
static FMDatabase *db;
@implementation SqliteModel
//单利
+(instancetype)initData{
if(!sql){
sql=[[SqliteModel alloc]init];
}
return sql;
}
//初始化数据库
-(void)initSql{
//创建路径
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接
NSString *file=[path stringByAppendingPathComponent:@“sqli.db”];
NSLog(@“file : %@”,file);
//创建数据库
db=[[FMDatabase alloc]initWithPath:file];
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 Model(ID integer primary key,title text,author_name text,url text)”];
//关闭数据库
[db close];
}
//添加数据
//-(void)addData:(Model *)data{
-(void)addData:(Model *)data{
//打开数据库
if([db open]){
//添加数据的sql语句:insert into 表名 values(null,?,?);
[db executeUpdate:[NSString stringWithFormat:@“insert into Model values(null,’%@’,’%@’,’%@’)”,data.title,data.author_name,data.url]];
NSLog(@“插入成功”);
}else{
NSLog(@“打开失败”);
}
[db close];
}
//删除数据
-(void)deleteData:(NSInteger)theid{
if([db open]){
//sql 语句: delete from 表名 where 表名的主键id = ?
[db executeUpdate:[NSString stringWithFormat:@“delete from Model where ID = ‘%ld’”,theid]];
NSLog(@“删除成功”);
}else{
NSLog(@“打开失败”);
}
[db close];
}
-(NSMutableArray *)getDataArray{
//创建可变数组
NSMutableArray *arr=[NSMutableArray array];
//创建集合
FMResultSet *set=[FMResultSet new];
if([db open]){
//sql 语句格式:select *from 表名
set=[db executeQuery:@“select *from Model”];
//判断有没有
while ([set next]) {
//初始化model
Model *MO=[Model new];
MO.ID=[set intForColumn:@“ID”];
MO.title=[set stringForColumn:@“title”];
MO.author_name=[set stringForColumn:@“author_name”];
MO.url=[set stringForColumn:@“url”];
NSLog(@"-----%@",MO);
[arr addObject:MO];
NSLog(@“查询成功”);
}
}else{
NSLog(@“打开失败”);
}
[db close];
return arr;
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值