第一 :
导航:
#import “AppDelegate.h”
#import “ViewController.h”
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
return YES;
}
1 把FMDB的第三方拖入到工程里面 创建DataModel 继承NSObject ,这个DataModel里面写的是解析的一些数据 在整个FMDB里面 属于表名 在DataModel里面必须定义一个NSInterger 类型的classID 属于主键ID
@interface DataModel : NSObject
@property(nonatomic,strong)NSString *imgsrc,*title,*mtime,*url;
// 设置一个主键ID
@property(nonatomic , assign)NSInteger classID;
@end
下面是DataModel.m里面的方法:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
第二:在定义一个Model类 继承于NSObject 在Model类里面主要写了FMDB的一些增,删,改,查
首先在Model.h里面定义一些方法 代码有:
#import <Foundation/Foundation.h>
#import “DataModel.h”
NS_ASSUME_NONNULL_BEGIN
@interface Model : NSObject
//单利方法
+(instancetype)initData;
//初始化数据库
-(void)initSql;
//初始化表格
-(void)initTable;
//添加 从解析里面的数据添加到数据库里面 所以是DataModel
-(void)addData:(DataModel *)data;
//删除
-(void)deletData:(NSInteger )theid;
//查询数据
-(NSMutableArray *)inquireArr;
//关闭数据库
-(void)closeSql;
@end
其次是Model.m里面的实现的一些方法
#import “Model.h”
#import “FMDatabase.h”
//创建静态变量
static Model *sql;
static FMDatabase *db;
@implementation Model
//初始化单利方法
+(instancetype)initData{
if (!sql) {
sql=[[Model alloc]init];
}
return sql;
}
//初始化数据库
-(void)initSql{
//获取Documents目录
NSString *str=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//拼接路径
NSString *fileName=[str stringByAppendingString:@"strmb.db"];
NSLog(@"%@",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,所有的数据类型); : 如果不存在DataModel(classID integer primary key,imgsrc blob,title text,url text),则创建表
[db executeUpdate:@"create table if not exists DataModel(classID integer primary key,imgsrc blob,title text,url text,mtime text)"];
//关闭数据库
[db close];
}
//添加数据
-(void)addData:(DataModel *)data{
//添加数据的sql语句:insert into 表名 values(null,?,?);
if ([db open]) {
[db executeUpdate:[NSString stringWithFormat:@“insert into DataModel values(null,’%@’,’%@’,’%@’,’%@’)”,data.imgsrc,data.title,data.url,data.mtime]];
NSLog(@“插入成功”);
}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]) {
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.mtime = [set stringForColumn:@"mtime"];
model.classID=[set intForColumn:@"classID"];
[arr addObject:model];
}
}else{
NSLog(@"查询失败");
}
[db close];
return arr;
}
@end
第三:创建TableViewCell继承UITableViewCell 进行拖拽xib约束
其次是拖拽到TableViewCell.h里面
#import <UIKit/UIKit.h>
#import “DataModel.h”
NS_ASSUME_NONNULL_BEGIN
@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *zhu;
@property (weak, nonatomic) IBOutlet UILabel *fu;
-(void)loadData:(DataModel *)mode;
然后在TableViewCell.m里面实现方法
-(void)loadData:(DataModel *)mode{
if (mode) {
self.zhu.text=mode.title;
self.fu.text=mode.mtime;
self.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mode.imgsrc]]];
}
}
第三:是在VC里面进行解析接口添加到表格里面
#import “ViewController.h”
#import <AFNetworking.h>
#import “TableViewCell.h”
#import “DataModel.h”
#import “oneViewController.h”
#import “twoViewController.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *DATAsource;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.DATAsource=[[NSMutableArray alloc]init];self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tab.delegate=self;
self.tab.dataSource=self;
self.navigationItem.title=@“好好学习”;
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“收藏界面” style:UIBarButtonItemStyleDone target:self action:@selector(click)];[self.view addSubview:self.tab];
//注册
[self.tab registerNib:[UINib nibWithNibName:@“TableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];[self AFN];
}
-(void)click{twoViewController *two=[twoViewController new];
[self.navigationController pushViewController:two animated:YES];
}
-(void)AFN{
AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
manger.responseSerializer=[AFHTTPResponseSerializer serializer];
[manger GET:@"http://c.m.163.com/nc/article/list/T1348648517839/0-20.html" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
NSDictionary *arr=[dic objectForKey:@"T1348648517839"];
for (NSDictionary *dic in arr) {
DataModel *model=[DataModel new];
[model setValuesForKeysWithDictionary:dic];
[self.DATAsource addObject:model];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tab reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.DATAsource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
//
//
// if (cell==nil) {
// cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:oj];
// }
DataModel *model=self.DATAsource[indexPath.row];
self.tab.rowHeight=150;
cell.zhu.text=model.title;
cell.fu.text=model.mtime;
cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgsrc]]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
oneViewController *one=[oneViewController new];
one.model= self.DATAsource[indexPath.row];
[self.navigationController pushViewController:one animated:YES];
}
@end
其次创建OneVC
在OneVC.h里面
引入 DataModel.h 进行属性传值
#import <UIKit/UIKit.h>
#import “DataModel.h”
NS_ASSUME_NONNULL_BEGIN
@interface oneViewController : UIViewController
@property(nonatomic,strong)DataModel *model;
@end
其次在OneVC.m里面写 网页视图 点击收藏显示添加成功
#import “oneViewController.h”
#import <WebKit/WebKit.h>
#import “Model.h”
@interface oneViewController ()
@property(nonatomic,strong)WKWebView *webview;
@end
@implementation oneViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.self.webview=[[WKWebView alloc]initWithFrame:self.view.frame];
self.navigationItem.title=@“网页视图”;
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“收藏” style:UIBarButtonItemStyleDone target:self action:@selector(click)];[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.url]]]];
[self.view addSubview:self.webview];
}
-(void)click{
[[Model initData]initSql];
[[Model initData]addData:self.model];
}
@end
最后一步创建TwoVC 是收藏的界面 里面显示收藏成功的数据 和一些删除数据的展示 在这里还可以运用自定义cell直接赋值 注册cell
#import “twoViewController.h”
#import “Model.h”
#import “TableViewCell.h”
@interface twoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *arr;
@end
@implementation twoViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tab.delegate=self;
self.tab.dataSource=self;
[self.view addSubview:self.tab];
//注册cell
[self.tab registerNib:[UINib nibWithNibName:@“TableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];self.arr=[[NSMutableArray alloc]init];
}
-(void)viewWillAppear:(BOOL)animated{
[[Model initData]initSql];
self.arr=[[Model initData]inquireArr];
[self.tab reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *oj=@"cell";
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
DataModel *model=self.arr[indexPath.row];
self.tab.rowHeight=150;
cell.zhu.text=model.title;
cell.fu.text=model.mtime;
cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgsrc]]];
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[[Model initData]initSql];
[[Model initData]deletData:[self.arr[indexPath.row]classID]];
[self.arr removeObject:self.arr[indexPath.row]];
[self.tab reloadData];
}
@end