YYModel解析与使用

Model.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
//内层  @interface Model : NSObject 留一个和类名一样的Model
@interface Model : NSObject
@property(nonatomic,strong)NSString *content;//主标题
@property(nonatomic,strong)NSString *updatetime;
@property(nonatomic,strong)NSString *url;

@end
//第二层
@interface twoModel : NSObject
@property(nonatomic,strong)NSArray *data;
@end
//第一层
@interface oneModel : NSObject
@property(nonatomic,strong)twoModel *result;
@end

Model.m

#import "Model.h"

@implementation Model

@end
///
@implementation twoModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
    // value should be Class or Class name.
    return @{@"data" : [Model class],
             };
    //同下
}
@end
////
@implementation oneModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
    // value should be Class or Class name.
    return @{@"result" : [twoModel class],
             };
    //根据名称找对应的值
}
@end

解析 controll

@interface jianViewController ()<UITableViewDelegate,UITableViewDataSource>//签订协议
@property(nonatomic,strong)UITableView  *tbv;
@property(nonatomic,strong)NSMutableArray  *dataSourceI;
@property(nonatomic,strong)oneModel *model;//在最外层model掏值!!
@end

 static NSString *str = @"cell";
@implementation jianViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //初始化
    [self.tbv reloadData];
    self.dataSourceI = [NSMutableArray new];
    _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tbv.delegate = self;
    _tbv.dataSource = self;
    [self.view addSubview:self.tbv];
    //注册表格
    [self.tbv registerClass:[jokeTableViewCell class] forCellReuseIdentifier:str];
    [self loadData];
   
}
-(void)loadData
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parame = @{@"key":@"eaaf69cdca2f46e403a264f5ef7cb74b",
                         @"time":@"1418816972"
                         };
[manager POST:@"http://v.juhe.cn/joke/content/list.php" parameters:parame progress:^(NSProgress * _Nonnull uploadProgress) {
    
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //        永远是最外层开始找,yymodel掏值格式
    self.model = [oneModel yy_modelWithJSON:responseObject];
    
    //把自定义的类型变成数组类型
    self.dataSourceI = self.model.result.data;
    //输出
    NSLog(@"======%@ 成功",self.dataSourceI);
        [self.tbv reloadData];

    NSLog(@"网络请求 成功");
 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"网络 失败");
 }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return   self.dataSourceI.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  160;
}
//表格  ‘单元格’
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //静态字符串标识
    
    jokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    [cell cellSetValue:self.dataSourceI[indexPath.row]];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    jiandetail *detail = [[jiandetail alloc]init];
     detail.strUrl = self.dataSourceI[indexPath.row];

    NSLog(@"====url: %@ ",detail.strUrl);

    [self.navigationController pushViewController:detail animated:YES];
  
}




@end

详情页

    #import <UIKit/UIKit.h>
    #import "Model.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface jiandetail : UIViewController
    @property(nonatomic,strong)Model* strUrl;
    
    @end
##  .m
#import "jianViewController.h"
#import "jiandetail.h"
#import <WebKit/WebKit.h>
@interface jiandetail ()

@end

@implementation jiandetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height+49)];
//    NSURL *webUrl = [NSURL URLWithString:self.strUrl.url];
//
//    NSURLRequest *request = [NSURLRequest requestWithURL:webUrl];
//
//    [webView loadRequest:request];
//    [self.view addSubview:webView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(right)];

}

- (void)right{

    [[DataManager shareManage]open];
    [[DataManager shareManage]create];
    [[DataManager shareManage]insert:self.strUrl];
    [[DataManager shareManage]close];
    
}

我的收藏实现页

.h

@interface collectionViewController : UIViewController

@property(nonatomic , strong)NSMutableArray *SeleArr;// 数组
@end

.m共用一个表格自定义

@interface collectionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *ojtable;// 表格

@end

@implementation collectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.ojtable];
}
-(UITableView *)ojtable{
    if (!_ojtable) {
        _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _ojtable.delegate = self;
        _ojtable.dataSource = self;
    }
    //注册表格,使用xib用registerNib
    [_ojtable registerClass:[jokeTableViewCell class] forCellReuseIdentifier:@"cell"];
    return _ojtable;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.SeleArr.count;
}
// 单元格数据
- (void)viewDidAppear:(BOOL)animated{
    //打开数据库
    [[DataManager shareManage]open];
    //把之前数据库的缓存数据给予新的数组
    self.SeleArr = [[DataManager shareManage]select];
    
    [self.ojtable reloadData];
    self.tabBarController.tabBar.hidden = NO;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    jokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.labelI.text = [self.SeleArr[indexPath.row]content];
   
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
// 左滑删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //打开数据库
    [[DataManager shareManage]open];
    //删除数据库
    [[DataManager shareManage]deleteData:[self.SeleArr[indexPath.row]content]];//根据model模型里的一个数据删除整个模型的数组数据
    
    //清空数据
    [self.SeleArr removeObject:self.SeleArr[indexPath.row]];
    [self.ojtable reloadData];
}

猜你喜欢

转载自blog.csdn.net/weixin_43364994/article/details/86248132