2018 2月4号

数据库

@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();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIView+SGFrame.h #import @interface UIView (SGFrame) @pro...
    超越时空的思念_8a22阅读 5,813评论 1 1
  • #import "DataManager.h" #import "AppDelegate.h" @interfac...
    小美788阅读 2,312评论 0 0
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 13,664评论 3 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,802评论 1 32
  • AppDelegate.m #import "AppDelegate.h" #import "ViewContro...
    哈喽Mm阅读 2,744评论 0 0