@property(nonatomic,strong)NSString* name;
@property(nonatomic,strong)NSString* imageUrl;
@property(nonatomic,strong)NSNumber* viewCount;
@property(nonatomic,strong)NSNumber* fileDuration;
videoMD;;;;;
+(id)videoCellWithCollectionView:(UICollectionView *)collectionView andIndexPath:(NSIndexPath *)indexPath{
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([self class])];
return [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([self class]) forIndexPath:indexPath];
}
///调用setModel的方法进行赋值
-(void)setCellDataWithModel:(VideoMD *)md{
[self.imgView sd_setImageWithURL:[NSURL URLWithString:md.imageUrl] placeholderImage:[UIImage imageNamed:@""]];
self.titleLabel.text = md.name;
self.numLabel.text = [NSString stringWithFormat:@"%@次",md.fileDuration];
///将NSNumber类型转换为 int类型
NSInteger minute = [md.viewCount integerValue]/60;
NSInteger second = [md.viewCount integerValue] - minute*60;
self.timeLabel.text = [NSString stringWithFormat:@"%ld:%ld",(long)minute,(long)second];
}
videocell;;;
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
self.mArr = [NSMutableArray array];
///网络解析请求数据
NSString* urlStr = @"http://123.126.40.109:7003/asmr/videos/A1100101.shtml";
[self sendRequestWithUrlStr:urlStr];
///网格的流水布局
UICollectionViewFlowLayout* cvfLayout = [[UICollectionViewFlowLayout alloc]init];
///垂直滚动
cvfLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
///最小的行间距与列间距
cvfLayout.minimumLineSpacing = 10;
cvfLayout.minimumInteritemSpacing = 10;
///网格的初始化
self.videoCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:cvfLayout];
[self.view addSubview:self.videoCollectionView];
self.videoCollectionView.delegate = self;
self.videoCollectionView.dataSource = self;
///下拉刷新的时候调用
self.videoCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
NSString* urlStr = @"http://123.126.40.109:7003/asmr/videos/A1100101.shtml";
///下拉刷新 网络重新请求数据
[self sendRequestWithUrlStr:urlStr];
}];
}
-(void)clickRightBtn{
NSLog(@"点击右侧按钮");
}
///网络解析的方法
-(void)sendRequestWithUrlStr:(NSString*)urlStr{
///开始下拉刷新
[self.videoCollectionView.mj_header beginRefreshing];
///开始请求网络数据
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功");
// NSLog(@"%@",responseObject);
///将二进制的数据转换为字典
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
///取出字典中的数组
NSArray* arr = [dic objectForKey:@"result"];
///循环数组中的字典
for (NSDictionary*dict in arr) {
///将字典转换成为模型
VideoMD* md = [VideoMD mj_objectWithKeyValues:dict];
NSLog(@"%@",md.imageUrl);
///将model存进数组中
[self.mArr addObject:md];
}
///异步刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
[self.videoCollectionView reloadData];
});
///结束下拉拉刷新
[self.videoCollectionView.mj_header endRefreshing];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败");
NSLog(@"%@",error);
///失败也结束下拉刷新
[self.videoCollectionView.mj_header endRefreshing];
}];
}
///网格的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.mArr.count;
}
///cell的赋值
-(UICollectionViewCell *)collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath )indexPath{
///调用tableViewCell
VideoCell cell = [VideoCell videoCellWithCollectionView:collectionView andIndexPath:indexPath];
VideoMD md = self.mArr[indexPath.row];
[cell setCellDataWithModel:md];
return cell;
}
///每一个格子的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake((self.view.frame.size.width-10)/2, (self.view.frame.size.height-20)/3);
}