准备工作
1,新建xib,删除画布上的view对象,并从库中拖入Collect View Header View
2,打开该对象的attribute inspector设置重用id
3,新建一个类,继承自UICollectionReusableView,并将xib中custome类设置为刚才新建的类
@interface ElectiveCourse :UIViewController
{
BOOL _isLoading;
NSMutableArray *_dataArray;
NSMutableDictionary *_coursePackDic;
}
@property(nonatomic,strong)UICollectionView *collectionView;
//-----
- (AppDelegate *)appdelegate;
-(void)refreshCoursePackData:(NSInteger)courseId;
- (instancetype) initCollectionView;
@end
//
// ElectiveCourse.m
// LearningEnglish
//
// Created by Xu Felicia on 14-5-15.
// Copyright (c) 2014年 xxx.com. All rights reserved.
//
#import "ElectiveCourse.h"
#import "subjectItem.h"
#import "studyWordController.h"
#import "MJRefresh.h"
#import "MeController.h"
#import "CollectViewHeaderView.h"
@interface ElectiveCourse ()
@end
@implementation ElectiveCourse
static NSString *strCellIdentify =@"subjectItemIdentify";
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title =@"选课";
}
return self;
}
static NSString *kCollectionViewHeaderIdentifier =@"cccc";
//
- (UICollectionViewFlowLayout *) flowLayout{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
if (iPhone5)
{
layout.itemSize = CGSizeMake(100, 100);
layout.minimumInteritemSpacing = 20;
layout.minimumLineSpacing = 40;
layout.sectionInset = UIEdgeInsetsMake( 20, 40, 20, 40);
}else
{
layout.itemSize = CGSizeMake(100, 100);
layout.minimumInteritemSpacing = 20;
layout.minimumLineSpacing = 20;
layout.sectionInset = UIEdgeInsetsMake( 20, 40, 20, 40);
}
return layout;
}
//最初一直没实现如下方法,导致莫名其妙的错误
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(320, 20);
}
- (void)addHeader
{
__unsafe_unretainedtypeof(self) vc =self;
//添加下拉刷新头部控件
[self.collectionView addHeaderWithCallback:^{
// 进入刷新状态就会回调这个Block
//模拟延迟加载数据,因此2秒后才调用)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[vc.collectionView reloadData];
//结束刷新
[vc.collectionView headerEndRefreshing];
});
}];
}
-(void)queryCourseData
{
[self refreshCoursePackData:1];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *topNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
topNavView.backgroundColor = [UIColor colorWithRed:33/255.0 green:169/255.0 blue:186/255.0 alpha:1];
[self.view addSubview:topNavView];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"r"] style:UIBarButtonItemStyleDone target:self action:@selector(showLeftView)];
NSString *strPath = [[NSBundle mainBundle] pathForResource:@"Home" ofType:@"plist"];
_coursePackDic = [NSDictionary dictionaryWithContentsOfFile:strPath];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
if (iPhone5)
{
layout.itemSize = CGSizeMake(100, 100);
layout.minimumInteritemSpacing = 6;
layout.minimumLineSpacing = 6;
layout.sectionInset = UIEdgeInsetsMake( 3, 40, 3, 40);
}else
{
layout.itemSize = CGSizeMake(100, 100);
layout.minimumInteritemSpacing = 6;
layout.minimumLineSpacing = 6;
layout.sectionInset = UIEdgeInsetsMake( 3, 40, 3, 40);
}
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 320, ScreenHeight -40) collectionViewLayout:layout];
self.collectionView.collectionViewLayout = layout;
self.collectionView.alwaysBounceVertical =YES;
self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.collectionView.dataSource =self;
self.collectionView.delegate =self;
[self.collectionView registerClass:[subjectItem class] forCellWithReuseIdentifier:strCellIdentify];
UINib *headerNib = [UINib nibWithNibName:NSStringFromClass([CollectViewHeaderView class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:headerNib forSupplementaryViewOfKind :UICollectionElementKindSectionHeader withReuseIdentifier: kCollectionViewHeaderIdentifier ]; //注册加载头
[self.view addSubview:self.collectionView];
[self queryCourseData];
[self addHeader];
// [self addFooter];
}
//
-(void)refreshCoursePackData:(NSInteger)courseId
{
NSString *skey = [NSStringstringWithFormat:@"p%d",courseId];
NSMutableDictionary *dic =_coursePackDic[skey];
_dataArray = [dicobjectForKey:@"data"];
self.title = dic[@"title"];
[self.collectionViewreloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return_dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *arr =_dataArray[section][@"courses"];
return arr.count;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
CollectViewHeaderView *view = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"cccc"forIndexPath:indexPath];
view.title = [NSString stringWithFormat:@"——%@——",_dataArray[indexPath.section][@"title"]];
return view;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
subjectItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strCellIdentify forIndexPath:indexPath];
/*
NSDictionary *dic = _dataArray[indexPath.row];
[cell initWithDic:dic];
int idx = [[dic objectForKey:@"imgidx"] intValue];
int64_t nId = 100002 + idx * 3;
*/
int64_t nId = 100002 + (arc4random() % 100 * 3);
NSString *strURL = [NSString stringWithFormat:@"http://lingo7.oss-cn-hangzhou.aliyuncs.com/images/%lld.jpg",nId];
NSURL *url = [NSURL URLWithString:strURL];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"image_home_replace"]];
cell.imageFlag.image = (cell.data.flag == 0?nil:[UIImage imageNamed:@"image_home_selflag"]);;
return cell;
}
@end