AFNet&coreData 持久化

创建Model
@interface Model : NSObject
@property (nonatomic,strong)NSString *imgsrc,*title;
@end

  • (void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

创建DataManager

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

@interface DataManager : NSObject
+(DataManager *)shareManager;
@property (nonatomic,strong)NSString *imgsrc,*title;
-(void)insert:(NSDictionary *)dic;

-(void)update:(Student *)student;

-(void)deleteData:(Model *)student;

-(NSArray *)select;
@end

NS_ASSUME_NONNULL_END
#import “DataManager.h”
#import “AppDelegate.h”
#import “Model.h”
@interface DataManager(){
//声明一个APPdelegate对象属性,调用里面的被管理对象上下文 保存方法
AppDelegate *myDelegate;
}
@end
@implementation DataManager
+(DataManager *)shareManager{
static DataManager *manager;
if (!manager) {
manager = [[DataManager alloc] init];
}
return manager;
}
-(void)insert:(NSDictionary *)dic{
//CoreData 数据的插入
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//创建实体描述对象
NSEntityDescription *description = [NSEntityDescription entityForName:@“Student” inManagedObjectContext:myDelegate.persistentContainer.viewContext];
//创建一个模型对象
Student *student = [[Student alloc] initWithEntity:description insertIntoManagedObjectContext:myDelegate.persistentContainer.viewContext];
student.title = dic[@“titleLab”];
student.imgsrc = dic[@“img”];

//对数据管理器的更改进行永久保存
[myDelegate saveContext];

}

-(void)update:(Student *)student{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[myDelegate saveContext];
}
-(NSArray *)select{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//查询数据
//1.创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@“Student”];
//2.执行查询请求
NSError *error = nil;
NSArray *result = [myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
NSMutableArray *muarry = [[NSMutableArray alloc] init];
for (Student *std in result) {
Model *model = [Model new];
model.title = std.title;
model.imgsrc = std.imgsrc;
[muarry addObject:model];

}

return [muarry copy];

}
-(void)deleteData:(Model *)student{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@“Student”];
//2.执行查询请求
NSError *error = nil;
NSArray *result = [myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
for (Student *std in result) {
if ([std.title isEqualToString:student.title]&&[std.imgsrc isEqualToString:student.imgsrc]) {

        [myDelegate.persistentContainer.viewContext deleteObject:std];
        [myDelegate saveContext];
    }
    
}

}

@end
创建view 自定义表格
@interface ZDYTableViewCell : UITableViewCell
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UIImageView *img;

  • (void)loadData:(Model *)mod;

  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 50, 50)];
    self.img.backgroundColor = [UIColor lightGrayColor];
    self.titleLab=[[UILabel alloc]initWithFrame:CGRectMake(70, 0, 300, 50)];
    // self.titleLab.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.img];

      [self.contentView addSubview:self.titleLab];
      [self setFrame];
    

    }
    return self;

}
-(void)setFrame{
}

  • (void)loadData:(Model *)mod{
    if (mod) {
    self.titleLab.text=mod.title;
    self.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mod.imgsrc]]];
    }

}
@end

创建控制器ViewController 我这个在系统写的
#import “ViewController.h”
#import “AFNetworking.h”
#import “Model.h”
#import “ZDYTableViewCell.h”
#import “DataManager.h”

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *table;
@property (nonatomic,strong)NSMutableArray *data;

@end
static NSString *oj = @“cell”;
@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    [self.table registerClass:[ZDYTableViewCell class] forCellReuseIdentifier:oj];

    self.data=[[NSMutableArray alloc]init];

    [self CreateAFNet];

    [self.view addSubview:self.table];
    [self.data addObjectsFromArray: [[DataManager shareManager] select]];
    }

  • (UITableView *)table{
    if (!_table) {
    _table=[[UITableView alloc]initWithFrame:self.view.frame];
    _table.delegate=self;
    _table.dataSource=self;
    }

    return _table;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZDYTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
    if (self.data.count>0) {
    DataManager *mod=self.data[indexPath.row];
    [cell loadData:mod];

    }
    return cell;
    }

  • (void)CreateAFNet{
    // return;
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@“http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html” parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"%@",responseObject);

      for ( NSDictionary *dic in responseObject[@"T1348647853363"]) {
          Model *mod=[[Model alloc]init];
          [mod setValuesForKeysWithDictionary:dic];
          [self.data addObject:mod];
          [[DataManager shareManager] insert:@{@"titleLab":mod.title,@"img":mod.imgsrc}];
    

}

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

}];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据库的
[[DataManager shareManager] deleteData:self.data[indexPath.row]];
//删除数据源
[self.data removeObject:self.data[indexPath.row]];
[self.table reloadData];
}

}
@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值