简单的收藏数据

首先导入我们封装的两个第三方文件夹
然后在infoplist文件进行开网
然后创建我们需要的Controller
首先是OneViewController
这个是我们第一个底部标签里面的东西,
然后创建一个SecViewController
这个是点击cell进入的界面
然后创建一个TwoViewController
这个里面是第二个底部标签,里面是我的收藏
然后创建一个自定义cell
OneTableViewCell
这里面是定义我们获取网络数据的地方
然后创建一个模型
DataModel
用来获取接口的参数
然后创建一个继承与Nsobject的
sqlite
然后开始写每个里面的代码
首先是ViewController


#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UINavigationController *one=[[UINavigationController alloc]initWithRootViewController:[OneViewController new]];
    one.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"首页" image:nil tag:1];
    UINavigationController *two=[[UINavigationController alloc]initWithRootViewController:[TwoViewController new]];
    two.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"收藏" image:nil tag:1];
    self.viewControllers=@[one,two];
    
}
@end

然后是sqlite.h里面

#import <Foundation/Foundation.h>
#import "DataModel.h"
NS_ASSUME_NONNULL_BEGIN

@interface Sqlite : NSObject
+(instancetype)initData;
-(void)initSql;
-(void)initTable;
-(void)addData:(DataModel *)data;
-(void)deletData:(NSInteger)theid;
-(NSMutableArray *)dataSource;
-(void)closeSql;
@end

NS_ASSUME_NONNULL_END

然后是sqite.m

#import "Sqlite.h"
#import "FMDatabase.h"
#import "DataModel.h"
static Sqlite *sqlData1;
static FMDatabase *db;
@implementation Sqlite
+(instancetype)initData{
    if (!sqlData1) {
        sqlData1=[[Sqlite alloc]init];
    }
    return sqlData1;
}
-(void)initSql{
    //获取DOcuments目录
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    //拼接路径
    NSString *fileName = [str stringByAppendingString:@"/new2.db"];
    //创建数据库
    db = [[FMDatabase alloc] initWithPath:fileName];
    
    if ([db open]) {
        
        NSLog(@"数据库打开成功");
        //调用初始化表格方法
        [self initTable];
    }else{
        
        NSLog(@"数据库打开失败");
        [db close];
    }
}
-(void)initTable{
    //初始化数据库表格的格式:create table if not exists 表名(主键id integer primary key,所有的数据类型);
    
    [db executeUpdate:@"create table if not exists DataModel(classid integer primary key,title text,imgsrc text,ptime 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.title,data.imgsrc,data.ptime ,data.url]];
        
    }else{
        
        NSLog(@"添加数据失败");
    }
    //关闭数据库
    [db close];
}
-(void)deletData:(NSInteger)theid{
    if ([db open]) {
        
        [db executeUpdate:[NSString stringWithFormat:@"delete from TitleModel where classid = '%ld'",theid]];
        
    }else{
        NSLog(@"删除数据失败");
    }
    //关闭数据库
    [db close];
}
-(NSMutableArray *)dataSource{
    //创建数据
    NSMutableArray *arr = [NSMutableArray array];
    //集合
    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.classid = [set intForColumn:@"classid"];
            model.title = [set stringForColumn:@"title"];
            model.imgsrc = [set stringForColumn:@"imgsrc"];
            model.ptime = [set stringForColumn:@"ptime"];
            model.url=[set stringForColumn:@"url"];
            
            //将msg对象添加到数据
            [arr addObject:model];
                 
        }
             
    }else{
        
        NSLog(@"查询失败");
    }
    
    [db close];
    return arr;
    
}
@end

然后是datamodel.h


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DataModel : NSObject
@property(nonatomic,copy)NSString *imgsrc;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *ptime;
@property(nonatomic,copy)NSString *url;
@property(nonatomic,assign)NSInteger classid;
@end

NS_ASSUME_NONNULL_END

然后是datamodel.m

#import "DataModel.h"

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

然后是OnetableviewCell.h


#import <UIKit/UIKit.h>
#import "DataModel.h"
NS_ASSUME_NONNULL_BEGIN

@interface OneTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView *imgV;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *detilLabel;
-(void)setModel:(DataModel *)model;
@end

NS_ASSUME_NONNULL_END

然后是OneTableViewCell.m

#import "OneTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation OneTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:@"cell"]) {
        [self addSubview:self.imgV];
        [self addSubview:self.titleLabel];
        [self addSubview:self.detilLabel];
    }
    return self;
}
-(UIImageView *)imgV{
    if (!_imgV) {
        _imgV=[[UIImageView alloc]initWithFrame:CGRectMake(15, 3, 130, 130)];
        
    }
    return _imgV;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(160,5, 240, 60)];
        _titleLabel.font=[UIFont systemFontOfSize:18];
        //_titleLabel.numberOfLines=0;
        
    }
    return _titleLabel;
}
-(UILabel *)detilLabel{
    if (!_detilLabel) {
        _detilLabel=[[UILabel alloc]initWithFrame:CGRectMake(160, 105, 350, 30)];
        _detilLabel.textColor=[UIColor lightGrayColor];
        _detilLabel.font=[UIFont systemFontOfSize:12];
    }
    return _detilLabel;
}
-(void)setModel:(DataModel *)model{
[self.imgV sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
    self.detilLabel.text=model.ptime;
    self.titleLabel.text=model.title;
}
@end

然后是OneViewController.m

#import "OneViewController.h"
#import "OneTableViewCell.h"
#import "FrameWork/Utils/NetWork/NetRequestManager.h"
#import "DataModel.h"
#import "UIImageView+WebCache.h"
#import "SecViewController.h"
@interface OneViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation OneViewController

- (void)viewDidLoad {
    self.dataSource=[NSMutableArray array];
    [super viewDidLoad];
    
    self.title = @"新闻";
    [self.view addSubview:self.tbv];
   
    [self loadData];
}
-(void)loadData{
    [NetRequestManager requestWithUrl:@"http://c.m.163.com/nc/article/list/T1348648517839/0-20.html" withRequestType:POST postParameters:nil success:^(NSDictionary *resDict) {
        NSLog(@"数据请求成功");
        NSArray *arr=resDict[@"T1348648517839"];
        for (NSDictionary *dic in arr) {
            DataModel *model=[[DataModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];
            [self.tbv reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}
-(UITableView *)tbv{
    if (!_tbv) {
        _tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tbv.delegate=self;
        _tbv.dataSource=self;
       
    }
    [_tbv registerClass:[OneTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    return _tbv;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.tbv.rowHeight=135;

   OneTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    DataModel *model=self.dataSource[indexPath.row];
    
    [cell setModel:model];
    cell.titleLabel.numberOfLines = 0;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecViewController *sec=[[SecViewController alloc]init];
    DataModel *model=self.dataSource[indexPath.row];
    sec.model=model;
    [self.navigationController pushViewController:sec animated:YES];
}
@end

然后是SecViewController.h

#import <UIKit/UIKit.h>
#import "DataModel.h"
NS_ASSUME_NONNULL_BEGIN

@interface SecViewController : UIViewController
@property(nonatomic,strong)DataModel *model;
@end

NS_ASSUME_NONNULL_END

然后是SecViewController.m

#import "SecViewController.h"
#import <WebKit/WebKit.h>
#import "Sqlite.h"
@interface SecViewController ()

@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
    WKWebView *webView=[[WKWebView alloc]initWithFrame:self.view.frame];
    NSURL *url=[NSURL URLWithString:self.model.url];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    [self.view addSubview:webView];
}
-(void)rightClick{
    [[Sqlite initData]initSql];
    [[Sqlite initData]addData:self.model];
     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStyleDone target:self action:nil];
}


@end

最后是TwoViewController.m

#import "TwoViewController.h"
#import "Sqlite.h"
#import "OneTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface TwoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    [self.view addSubview:self.tab];
}
-(void)viewWillAppear:(BOOL)animated{
    [[Sqlite initData]initSql];
    self.dataSource=[[Sqlite initData]dataSource];
    [self.tab reloadData];
}
-(UITableView *)tab{
    if (!_tab) {
        _tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tab.delegate=self;
        _tab.dataSource=self;
    }
    [_tab registerClass:[OneTableViewCell class] forCellReuseIdentifier:@"cell"];
    return _tab;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.tab.rowHeight=135;
   OneTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setModel:self.dataSource[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [[Sqlite initData]initTable];
   
    [[Sqlite initData]deletData:[self.dataSource[indexPath.row]classid]];
    [self.dataSource removeObject:self.dataSource[indexPath.row]];
    [self.tab reloadData];
    
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值