#import <JSONModel/JSONModel.h>
@interface WX_FriendZonePeiTuModel :JSONModel
// 朋友圈配图
//单张
@property (nonatomic,copy)NSString *pic;
// 多张
strong)NSArray *data;
copy)NSString *orders;
copy)NSString *pid; // 后台为id,用pid代替
copy)NSString *name;
@end
.m文件
#import "WX_FriendZonePeiTuModel.h"
@implementation WX_FriendZonePeiTuModel
// pid代替id
+(JSONKeyMapper *)keyMapper {
return [[JSONKeyMapperalloc]initWithModelToJSONDictionary:@{
@"pid":@"id"
}];
}
// 所有的属性都为可选类型,
+(BOOL)propertyIsOptional:(NSString *)propertyName {
/* 可以设置某个属性为可选,下面的代码意为name为可选, 后台数据中没有name键
if ([propertyName isEqualToString:@"name"]) {
return YES;
}
*/
// 所有属性可选
returnYES;
}
@end
WX_FriendZonePeiTuModel *model = [[WX_FriendZonePeiTuModelalloc]initWithDictionary:dicterror:nil];
dict为解析的字典
#import <JSONModel/JSONModel.h>
// 内层模型
@interface WX_MoudleItemModel :JSONModel
@property (nonatomic,strong)NSArray *data;
copy)NSString *orders;
copy)NSString *addTime;
copy)NSString *content;
copy)NSString *fid;
copy)NSString *name;
copy)NSString *type;
strong)NSArray <NSDictionary *> *pics;
@end
// 外层模型
@protocolWX_MoudleItemModel; // 内层模型协议!!!!
@interface WX_MoudleSectionModel :JSONModel
copy)NSString *addTime;// section时间
@property (nonatomic,strong)NSArray <> *lists; // 数组元素为内层模型!!!
copy)NSString *rownum;
.m文件
#import "WX_MoudleSectionModel.h"
@implementation WX_MoudleItemModel
+(BOOL)propertyIsOptional:(NSString *)propertyName {
returnYES;
}
@end
@implementation WX_MoudleSectionModel
+(BOOL)propertyIsOptional:(NSString *)propertyName {
returnYES;
}
@end
#import "WX_FriendMutipleView.h"
#import <UIKit/UIKit.h>
@class WX_FriendZonePeiTuModel;
@interface WX_FriendZonePeiTuCell :UICollectionViewCell
/** weitu */
strong)UIImageView *peiTuImV;
/** content */
strong)UILabel *content;
/** model */
strong)WX_FriendZonePeiTuModel *model;
// 内层模型为collectionView的cell(外层模型为sectionHeader,写在reusebleView里)
strong)WX_MoudleItemModel *item;
strong)WX_FriendMutipleView *mutiView;
cell 的.m文件里
-(void)setItem:(WX_MoudleItemModel *)item {
/*
你的代码...
*/
}2.3 viewController文件
.m文件
#import "WX_MyFriendZoneViewController.h"
#import "WX_MoudleSectionModel.h" // 引入外层模型.h文件
@class WX_MoudleItemModel; // 内层模型
模型解析
-(void)xxx {
if (data.count) {
for (NSDictionary *modelDictin data) {
WX_MoudleSectionModel *model = [[WX_MoudleSectionModelalloc]initWithDictionary:modelDicterror:nil];
[weakSelf.peiTuArraddObject:model];
}
}
}
- (__kindofUICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
WX_MoudleSectionModel *model =self.peiTuArr[indexPath.section]; // 外层
WX_MoudleItemModel *items = model.lists[indexPath.item]; // 内层
WX_FriendZonePeiTuCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:peiTuCellforIndexPath:indexPath];
cell.item = items; // cell赋值
return cell;
}
大功告成
我遇到的坑:
@H_404_2@ JSONModel 模型嵌套 @H_404_2@ Cell中声明的时候,不要@class ,要#import外层模型.h文件 ,并且在ViewController中引入外层模型的.h + @class 内层模型; @H_404_2@ 不然会初现 @H_404_2@ -[__NSDictionaryM content]: unrecognized selector sent to instance xxxx @H_404_2@