**
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger,RequestType) {
GET,
POST
};
typedef void(^RequestFinish) (NSData *data);
typedef void(^RequestError) (NSError *error);
@interface NetWorkRequstManger : NSObject
+(void)requestWithType:(RequestType)type urlString:(NSString *)urlString ParDic:(NSDictionary *)ParDic finish:(RequestFinish)finish err:(RequestError)err;
@end
**
#import "NetWorkRequstManger.h"
@implementation NetWorkRequstManger
#pragma mark - 封装成+号
+(void)requestWithType:(RequestType)type urlString:(NSString *)urlString ParDic:(NSDictionary *)ParDic finish:(RequestFinish)finish err:(RequestError)err
{
NetWorkRequstManger *manager = [[NetWorkRequstManger alloc] init];
[manager requestWithType:type urlString:urlString ParDic:ParDic finish:finish err:err];
}
-(void)requestWithType:(RequestType)type urlString:(NSString *)urlString ParDic:(NSDictionary *)ParDic finish:(RequestFinish)finish err:(RequestError)err
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
if (type == POST) {
[mRequest setHTTPMethod:@"POST"];
if (ParDic.count > 0) {
NSData *data = [self DicatoData:ParDic];
[mRequest setHTTPBody:data];
}
}
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask *task = [session dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
finish(data);
}else
{
err(error);
}
}];
[task resume];
}
#pragma mark - 把参数字典转化为NSData的私有方法
-(NSData *)DicatoData:(NSDictionary *)dic
{
NSMutableArray *array = [NSMutableArray array];
for (NSString *key in dic) {
NSString *KeyAndValue = [NSString stringWithFormat:@"%@ = %@",key,dic[key]];
[array addObject:KeyAndValue];
}
NSString *parStr = [array componentsJoinedByString:@"&"];
NSLog(@"%@",parStr);
NSData *data = [parStr dataUsingEncoding:NSUTF8StringEncoding];
return data;
}
@end
**
.m文件
@implementation BaseModel
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"key = %@",key);
}
@end
#import "BaseModel.h"
@interface ListModel : BaseModel
@property(nonatomic,strong)NSString *coverimg;
@property(nonatomic,strong)NSString *enname;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *type;
@end
#import "ViewController.h"
#import "ListModel.h"
#import "NetWorkRequstManger.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *listArray;
@end
@implementation ViewController
-(NSMutableArray *)listArray
{
if (!_listArray) {
self.listArray = [NSMutableArray array];
}
return _listArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createCollcetionView];
[self requestData];
}
-(void)requestData
{
[NetWorkRequstManger requestWithType:GET urlString:READLIST_URL ParDic:@{@"client" :@"1"} finish:^(NSData *data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dataDic = [dic objectForKey:@"data"];
NSArray *listArray = [dataDic objectForKey:@"list"];
for (NSDictionary *dic in listArray) {
ListModel *model = [[ListModel alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_listArray addObject:model];
}
[self performSelectorOnMainThread:@selector(doMain) withObject:nil waitUntilDone:YES];
} err:^(NSError *error) {
NSLog(@"%@",error);
}];
}
-(void)doMain
{
[_collectionView reloadData];
}
#pragma mark - 创建CollectionView
-(void)createCollcetionView
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 2;
layout.minimumInteritemSpacing = 2;
layout.itemSize = CGSizeMake(ScreenWith/3-10, ScreenWith/3-10);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, self.view.bounds.size.height-150) collectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:self.collectionView];
}
#pragma mark - collectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.listArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
ListModel *model = [_listArray objectAtIndexedSubscript:indexPath.row];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:cell.bounds];
nameLabel.text = model.name;
cell.backgroundColor = [UIColor redColor];
[cell addSubview:nameLabel];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
