数据库
@property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *imgsrc;
modle
@property (nonatomic,strong)NSString *imgsrc,*title;
data
#import "Student+CoreDataClass.h"
#import "Model.h"
+(DataManager*)shareManager;
@property (nonatomic,strong)NSString *imgsrc,*title;
-(void)insert:(NSDictionary*)dic;
-(void)update:(Student*)student;
-(void)deleteData:(Model*)student;
-(NSArray*)select;
.m
#import "AppDelegate.h"
#import "Model.h"
{
//声明一个APPdelegate对象属性,调用里面的被管理对象上下文 保存方法
AppDelegate*myDelegate;
}
+(DataManager*)shareManager{
staticDataManager*manager;
if(!manager) {
manager = [[DataManageralloc]init];
}
returnmanager;
}
-(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.viewContextexecuteFetchRequest:requesterror:&error];
NSMutableArray *muarry = [[NSMutableArray alloc] init];
for(Student*stdinresult) {
Model*model = [Modelnew];
model.title= std.title;
model.imgsrc= std.imgsrc;
[muarryaddObject:model];
}
return[muarrycopy];
}
-(void)deleteData:(Model*)student{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
//2.执行查询请求
NSError*error =nil;
NSArray*result = [myDelegate.persistentContainer.viewContextexecuteFetchRequest:requesterror:&error];
for(Student*stdinresult) {
if([std.titleisEqualToString:student.title]&&[std.imgsrcisEqualToString:student.imgsrc]) {
[myDelegate.persistentContainer.viewContext deleteObject:std];
[myDelegatesaveContext];
}
}
}
cell
#import "Model.h"
#import "DataManager.h"
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UIImageView *img;
- (void)loadData:(Model*)mod;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
if(self=[superinitWithStyle:stylereuseIdentifier: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.contentViewaddSubview:self.img];
[self.contentViewaddSubview:self.titleLab];
[selfsetFrame];
}
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]]];
}
}
.m
#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
staticNSString*oj =@"cell";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.table registerClass:[ZDYTableViewCell class] forCellReuseIdentifier:oj];
self.data=[[NSMutableArray alloc]init];
[self CreateAFNet];
[self.viewaddSubview: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];
[cellloadData:mod];
}
returncell;
}
- (void)CreateAFNet{
// return;
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
[managerGET:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html" parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSLog(@"%@",responseObject);
for(NSDictionary*dicinresponseObject[@"T1348647853363"]) {
Model*mod=[[Modelalloc]init];
[modsetValuesForKeysWithDictionary:dic];
[self.dataaddObject:mod];
[[DataManagershareManager]insert:@{@"titleLab":mod.title,@"img":mod.imgsrc}];
}
[self.tablereloadData];
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"%@",error);
}];
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
return 60;
}
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据库的
[[DataManagershareManager]deleteData:self.data[indexPath.row]];
//删除数据源
[self.dataremoveObject:self.data[indexPath.row]];
[self.tablereloadData];
}
}
app.h
@property(readonly,strong)NSPersistentContainer*persistentContainer;
- (void)saveContext;
app.m
- (void)saveContext {
NSManagedObjectContext *context = self.persistentContainer.viewContext;
NSError*error =nil;
if([contexthasChanges] && ![contextsave:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}
网友评论