收藏(有图)

//
// DataModel.h
// YueKaoProject
//
// Created by 2018/9/4
// Copyright © 2018/9/4 All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DataModel : NSObject

// 设置一个主键ID
@property(nonatomic , assign)NSInteger classID;

// 图片
@property(nonatomic , strong)NSString *imgsrc;

// 标题
@property(nonatomic , strong)NSString *title;

// 跳转url
@property(nonatomic , strong)NSString *url;

@end

NS_ASSUME_NONNULL_END
//
// DataModel.m
// YueKaoProject
//
// Created by 2018/9/4…
// Copyright © 2018/9/4… All rights reserved.
//

#import “DataModel.h”

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

}

@end
//
// SqliteModel.h
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4… All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface SqliteModel : NSObject

// 单利方法
+(instancetype)initData;
// 初始化数据库
-(void)initSql;
// 初始化表格
-(void)initTable;
// 添加
-(void)addData:(DataModel *)data;
// 删除数据
-(void)deletData:(NSInteger)theid;
// 查询数据
-(NSMutableArray *)inquireArr;
// 关闭数据库
-(void)closeSql;

@end

NS_ASSUME_NONNULL_END
//
// SqliteModel.m
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4. All rights reserved.
//

#import “SqliteModel.h”

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

@implementation SqliteModel

// 初始化单利方法
+(instancetype)initData{

if(!sql){
    
    sql = [[SqliteModel alloc]init];

}

return sql;

}

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

// 获取Documents目录
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// 拼接路径
NSString *fileName = [str stringByAppendingString:@"news.db"];
// 创建数据库
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 DataModel(classID integter primary key,imgsrc blob,title text,url text)"];
// 关闭数据库
[db close];

}

// 添加数据
-(void)addData:(DataModel *)data{

if ([db open]) {
    
    //添加数据的sql语句:insert into 表名 values(null,?,?);
    [db executeUpdate:[NSString stringWithFormat:@"insert into DataModel values(null,'%@','%@','%@')",data.imgsrc,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 DataModel where classID = '%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 DataModel"];
    // 判断有没有东西
    while ([set next]) {
        
        DataModel *model = [[DataModel alloc]init];
        model.imgsrc = [set stringForColumn:@"imgsrc"];
        model.title = [set stringForColumn:@"title"];
        model.url = [set stringForColumn:@"url"];
        model.classID = [set intForColumn:@"classID"];
        [arr addObject:model];
    }
    
}else{
    NSLog(@"查询失败");
}
[db close];
return arr;

}

-(void)closeSql{

}

@end
//
// NewsTableViewCell.h
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4… All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface NewsTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

-(void)loadDataModel:(DataModel *)model;

@end

NS_ASSUME_NONNULL_END
//
// NewsTableViewCell.m
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright ©2018/9/4. All rights reserved.
//

#import “NewsTableViewCell.h”

@implementation NewsTableViewCell

-(void)loadDataModel:(DataModel *)model{

if(model){
    NSLog(@"title====%@",model.title);
    self.titleLabel.text = model.title;
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
}

}

@end
加入xib img lable

//
// CollectViewController.h
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CollectViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
//
// CollectViewController.m
// YueKaoProject
//
// Created by 岳飞洋(关机) on 2019/1/9.
// Copyright © 2019 岳飞洋. All rights reserved.
//

#import “CollectViewController.h”

@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *tbv;
@property(nonatomic , strong)NSMutableArray *array;
@end

@implementation CollectViewController

  • (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{

[[SqliteModel initData]initSql];
self.array = [[SqliteModel initData]inquireArr];
[self.tbv reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];

[cell loadDataModel:self.array[indexPath.row]];

return cell;

}

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

[[SqliteModel initData]initSql];

[[SqliteModel initData]deletData:[self.array[indexPath.row]classID]];
//删除数据
[self.array removeObject:self.array[indexPath.row]];

//[self.array removeAllObjects];

[self.tbv reloadData];

// DataModel *mo = [[DataModel alloc]init];
// [[SqliteModel initData]initSql];
//
// [[SqliteModel initData]deletData:mo.classID];
// self.array = [[SqliteModel initData]inquireArr];
// [self.tbv reloadData];

}

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

CollectNextViewController *collect = [CollectNextViewController new];
DataModel *data = self.array[indexPath.row];
collect.model = data;
[self.navigationController pushViewController:collect animated:YES];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}

@end
/
// CollectNextViewController.h
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CollectNextViewController : UIViewController

@property(nonatomic , strong)DataModel *model;

@end

NS_ASSUME_NONNULL_END
//
// CollectNextViewController.m
// YueKaoProject
//
// Created by 2018/9/4. on 2019/1/9.
// Copyright © 2018/9/4. All rights reserved.
//

#import “CollectNextViewController.h”

@interface CollectNextViewController ()

@end

@implementation CollectNextViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    //网页视图 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.model.url];
    //创建请求对象
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //网页视图加载网页
    [webview loadRequest:request];
    //添加到当前视图上
    [self.view addSubview:webview];

}

@end
//
// ViewController.m
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4… All rights reserved.
//

#import “ViewController.h”

@interface ViewController ()<UITableViewDelegate , UITableViewDataSource>{
NSMutableArray *dataSource;
}

@property(nonatomic , strong)UITableView *table;

@end

static NSString *oj = @“cell”;

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化
    dataSource = [[NSMutableArray alloc]init];

    self.title = @“数据库”;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“收藏” style:UIBarButtonItemStyleDone target:self action:@selector(collect)];

    self.table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource =self;
    [self.view addSubview:self.table];

    // 注册
    [self.table registerNib:[UINib nibWithNibName:@“NewsTableViewCell” bundle:nil] forCellReuseIdentifier:oj];

    [self loadData];

}

-(void)loadData{

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:YULE_PATH parameters:self progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
    NSDictionary *dict = responseObject;
    for (NSDictionary *dic in [dict objectForKey:@"T1348648517839"]) {
        DataModel *mod = [[DataModel alloc]init];

// [mod setValuesForKeysWithDictionary:dic];
// NSLog(@“title===%@”,[dic objectForKey:@“title”]);
mod.title = [dic objectForKey:@“title”];
mod.url = [dic objectForKey:@“url”];
mod.imgsrc = [dic objectForKey:@“imgsrc”];
[self->dataSource addObject:mod];

    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"数据请求成功");

        [self.table reloadData];
    });
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"请求失败");
}];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//NSLog(@"%@",dataSource);

// NSLog(@"%ld",dataSource.count);
return dataSource.count;
}

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

NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:oj];

[cell loadDataModel:dataSource[indexPath.row]];

return cell;

}

-(void)collect{

CollectViewController *collect = [[CollectViewController alloc]init];
[self.navigationController pushViewController:collect animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 120;

}

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

NextViewController *next = [[NextViewController alloc]init];

DataModel *data = dataSource[indexPath.row];
next.model = data;
[self.navigationController pushViewController:next animated:YES];

}
@end
//
// NextViewController.h
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4… All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface NextViewController : UIViewController

@property(nonatomic , strong)DataModel *model;

@end

NS_ASSUME_NONNULL_END
//
// NextViewController.m
// YueKaoProject
//
// Created by 2018/9/4.
// Copyright © 2018/9/4… All rights reserved.
//

#import “NextViewController.h”

@interface NextViewController ()

@end

@implementation NextViewController

  • (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.model.url];
    //创建请求对象
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //网页视图加载网页
    [webview loadRequest:request];
    //添加到当前视图上
    [self.view addSubview:webview];

}

-(void)collect{

[[SqliteModel initData]initSql];

// NSLog(@"=======%@",self.model);
[[SqliteModel initData]addData:self.model];

}

@end
//
// PrefixHeader.pch
// YueKaoProject
//
// Created by ) on 2018/9/4.
// Copyright ©2018/9/4. . All rights reserved.
//

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import “DataModel.h”
#import “SqliteModel.h”
#import “NewsTableViewCell.h”
#import “CollectViewController.h”
#import “CollectNextViewController.h”
#import “ViewController.h”
#import “NextViewController.h”
#import <UIImageView+WebCache.h>
#import “AFNetworking.h”
#import “UIKit+AFNetworking.h”
#import <WebKit/WebKit.h>
#import “FMDatabase.h”
#import “SqliteModel.h”

#define YULE_PATH @“http://c.m.163.com/nc/article/list/T1348648517839/0-20.html
#define YULE_KEY @“T1348648517839”

#endif /* PrefixHeader_pch */

猜你喜欢

转载自blog.csdn.net/weixin_44097667/article/details/86559065