ViewController.m#
//
// ViewController.m
// UICollectionView
//
//
#import "ViewController.h"
#import "MovieModel.h"
#import "MovieCollectionViewCell.h"
#define MOVIE_URL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableDictionary *dataDictionary;
@property(nonatomic, strong)NSMutableArray *dataArray;
@end
@implementation ViewController
NSString *identifier = @"cell";
NSString *headerIdentifer = @"header";
NSString *footerIdentifier = @"footer";
-(void)parserData{
self.dataDictionary = [NSMutableDictionary dictionary];
self.dataArray = [NSMutableArray arrayWithCapacity:26];
//1.准备URl
NSURL *url = [NSURL URLWithString:MOVIE_URL];
//2.准备session
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.创建session
NSURLSession *session = [NSURLSession sharedSession];
//4.创建dataTask
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data != nil) {
self.dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
for (NSMutableDictionary *dic in _dataDictionary[@"result"]) {
MovieModel *MM = [MovieModel new];
[MM setValuesForKeysWithDictionary:dic];
[_dataArray addObject:MM];
}
NSLog(@"222222%ld",_dataArray.count);
for (MovieModel *M in _dataArray) {
NSLog(@"%@",M);
}
}
//返回主线程刷新数据
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}];
//5.执行
[task resume];
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//设置最小行间距
flowLayout.minimumLineSpacing = 21;
//设置最小列间距
flowLayout.minimumInteritemSpacing = 10;
//设置item大小
flowLayout.itemSize = CGSizeMake(110, 160);
//滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置集合视图内边距(顺序:上左下右)
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
//header区域大小
flowLayout.headerReferenceSize = CGSizeMake(414, 64);
//footer区域大小//设置了他们的大小就要把相应的设置设置完,
flowLayout.footerReferenceSize = CGSizeMake(414, 50);
//创建UICollectionView之前必须先创建布局对象
_collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];//必须要有布局格式
// 设置属性
_collectionView.backgroundColor = [UIColor whiteColor];
//是否显示水平指示标
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[self.view addSubview:_collectionView];
//注册cell
[_collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:identifier];
//注册增补视图(相当于tableView的header footer)
//header
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifer];
//footer
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
__weak typeof(self)temp = self;
dispatch_queue_t queue = dispatch_queue_create("GCD", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[temp parserData];
});
}
//集合视图中section个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
//section中返回item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"===%ld",_dataArray.count);
return [_dataArray count];
}
//Cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.contentView.backgroundColor =[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
MovieModel *mm = _dataArray[indexPath.row];
cell.model = mm;
return cell;
}
//返回增补视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//如果是头视图
if (kind ==UICollectionElementKindSectionHeader) {
//从重用池里取
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifer forIndexPath:indexPath];
headerView.backgroundColor = [UIColor blueColor];
return headerView;
}
//
if (kind == UICollectionElementKindSectionFooter) {
//
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
footerView.backgroundColor = [UIColor blackColor];
return footerView;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
MovieModel.h#
//
// MovieModel.h
// 豆瓣分组Cinema
//
//
#import <Foundation/Foundation.h>
@interface MovieModel : NSObject
@property(nonatomic, strong)NSString *movieName;
@property(nonatomic, strong)NSString *pic_url;
@property(nonatomic,strong)NSString *movieId;
@property(nonatomic, strong)NSString *poster;//海报
@property(nonatomic,strong)NSString *rating;//评分
@property(nonatomic,strong)NSString *rating_count;//评分人数
@property(nonatomic,strong)NSString *release_date;//放映时间
@property(nonatomic,strong)NSString *runtime;//时长
@property(nonatomic,strong)NSString *genres;//种类
@property(nonatomic,strong)NSString *film_locations;//拍摄地
@property(nonatomic,strong)NSString *plot_simple;//情节
@property(nonatomic,strong)NSString *actors;
@end
MovieModel.m#
//
// MovieModel.m
// 豆瓣分组Cinema
//
.
//
#import "MovieModel.h"
@implementation MovieModel
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
// if ([key isEqualToString:@"pic_url"]) {
// _imag = value;
// }
//
// if ([key isEqualToString:@"poster"]) {
// _imag = value;
// }
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@", _movieName];
}
@end
MovieCollectionViewCell.h#
//
// MovieCollectionViewCell.h
// UI_21_UICollectionView
//
//
#import <UIKit/UIKit.h>
#import "MovieModel.h"
@interface MovieCollectionViewCell : UICollectionViewCell
@property(nonatomic, strong)UIImageView *movieV;
@property(nonatomic, strong)UILabel *moviNameL;
@property(nonatomic, strong)MovieModel *model;
@end
MovieCollectionViewCell.m#
//
// MovieCollectionViewCell.m
// UI_21_UICollectionView
//
//
#import "MovieCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@implementation MovieCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addCustomView];
}
return self;
}
-(void)addCustomView{
self.movieV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 110, 140)];
_movieV.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:_movieV];
self.moviNameL = [[UILabel alloc] initWithFrame:CGRectMake(0, 140, 110, 20)];
// _moviNameL.backgroundColor = [UIColor blackColor];
[self.contentView addSubview:_moviNameL];
}
-(void)setModel:(MovieModel *)model{
[self.movieV sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"11.jpg"] ];
self.moviNameL.text = model.movieName;
}
@end
网友评论