数据解析 持久化数据收藏

首先导入三方
创建控制器
#import <AFNetworking.h>
#import “MyTableViewCell.h”
#import “DailyModel.h”
#import “Model.h”
#import “ShouCangViewController.h”

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)UITableView *tbV;
@property (nonatomic , strong)NSMutableArray *array;

@end

@implementation NewsViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    //初始化数组
    self.array = [[NSMutableArray alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @“新闻头条”;

    _tbV = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    _tbV.delegate = self;
    _tbV.dataSource = self;

    [self.view addSubview:self.tbV];

    //注册
    [self.tbV registerNib:[UINib nibWithNibName:@“MyTableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];

    [self loadData];

}

-(void)loadData{
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];

manger.responseSerializer = [AFHTTPResponseSerializer serializer];

[manger GET:@"http://v.juhe.cn/joke/content/list.php?key=eaaf69cdca2f46e403a264f5ef7cb74b&time=1418816972" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
    
    NSDictionary *dic1 = [dic objectForKey:@"result"];
    
    NSDictionary *arr = [dic1 objectForKey:@"data"];
    
    for (NSDictionary *dic in arr) {
        DailyModel *model = [DailyModel new];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.array addObject:model];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tbV reloadData];
    });
    
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"请求失败");
    
    
}];

}

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

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

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {
    cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}

_tbV.rowHeight = 200;

DailyModel *model = self.array[indexPath.row];

cell.lab1.text = model.content;

cell.lab2.text = model.updatetime;



return cell;

}

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

[[Model initData]addData:@{@"name":[self.array[indexPath.row] content],@"time":[self.array[indexPath.row] updatetime]}];



[self.tbV deselectRowAtIndexPath:indexPath animated:YES];

}

@end

第二个控制器

#import “Model.h”
#import “DailyModel.h”
#import “Entity+CoreDataClass.h”
@interface ShouCangViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tab;

@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation ShouCangViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @“我的收藏”;

    self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tab.delegate=self;
    self.tab.dataSource=self;

    [self.view addSubview:self.tab];

    self.array=[[NSMutableArray alloc]init];
    }

//视图即将出现

-(void)viewWillAppear:(BOOL)animated{

self.array=[[Model initData]array];

[self.tab reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.array.count;

}

-(void)dian{

DailyModel *mode=[DailyModel new];


NSDictionary *dic=@{@"name":mode.content,@"time":mode.updatetime};



[[Model initData]addData:dic];

[self.navigationController popViewControllerAnimated:YES];

}

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

static NSString *oj=@"cell";


UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];


if (cell==nil) {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:oj];
    
    
}

self.tab.rowHeight=200;

//    DataModel *mod=_array[indexPath.row];


Entity *cla=_array[indexPath.row];

//    DataModel *model=self.array[indexPath.row];

cell.textLabel.text=cla.name;
cell.textLabel.numberOfLines=0;
cell.detailTextLabel.text=cla.time;


return cell;

}

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

//创建实体
Entity *cla=self.array[indexPath.row];

//调用删除数据的方法
[[Model initData]deleteData:cla];

//重新获取数据
self.array=[[Model initData]array];

//刷新表格
[self.tab reloadData];

}

@end

创建自定义cell
.h
#import “DailyModel.h”
NS_ASSUME_NONNULL_BEGIN

@interface MyTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
-(void)setDataModel:(DailyModel *)model;

.m

  • (void)setDataModel:(DailyModel *)model{

    if (model) {

      self.lab1.text=model.content;
      self.lab2.text=model.updatetime;
    

    }

}

创建DailyModel
.h
@property(nonatomic,strong)NSString *content;
@property(nonatomic,strong)NSString *updatetime;
@property(nonatomic,strong)NSString *hashId;
.m
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

数据化 model
.h
#import “AppDelegate.h”
#import “Entity+CoreDataClass.h”

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject{
AppDelegate *app;
}

//创建单利方法

//单利方法
+(instancetype)initData;
//添加数据
-(void)addData:(NSDictionary * )dic;
//删除数据
-(void)deleteData:(Entity *)data;
//修改
-(void)upData;
//查询
-(NSMutableArray *)array;

@end

.m

#import “DailyModel.h”

//创建静态变量
static Model *da;
@implementation Model

+(instancetype)initData{
if (!da) {
da = [Model new];
}

return da;

}

-(void)addData:(NSDictionary *)dic{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//创建实体对象  NSEntityDescription(实体描叙对象)
Entity *cla = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];

cla.name=dic[@"name"];
cla.time=dic[@"time"];
//
[app saveContext];

}

//删除数据

  • (void)deleteData:(Entity *)data{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //删除
    [app.persistentContainer.viewContext deleteObject:data];
    //保存数据
    [app saveContext];

}

//修改数据

  • (void)upData{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //保存数据
    [app saveContext];

}

//查询数据

  • (NSMutableArray *)array{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //创建请求数据对象
    NSFetchRequest *requst = [[NSFetchRequest alloc] init];
    //创建实体描述类
    NSEntityDescription *ent = [NSEntityDescription entityForName:@“Entity” inManagedObjectContext:app.persistentContainer.viewContext];
    //获取数据
    [requst setEntity:ent];
    //从实体中获取数据
    NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:requst error:nil];

    return [arr mutableCopy];

}

创建实体
info.plist 开通网络

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值