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