IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView
其实看标题就知道是需要继承于UICollectionReusableView,实现一个满足自己需求的视图.那么如何操作了,看下面代码:
ViewController.m文件中 #import "ViewController.h" #import "LSHControl.h" #import "SHCollectionReusableView.h" #import "SHCollectionViewCell.h" #define SHCollectionViewCellIdetifiler @"CollectionViewCell" #define SHCollectionReusableViewHeader @"CollectionHeader" #define SHCollectionReusableViewFooter @"CollectionFooter" @interface ViewController (){ NSArray *recipeImages; NSArray *hearderTitleArray; } @property(nonatomic,strong) UICollectionView *rightCollectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *mainDishImages = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", nil]; NSArray *drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg", @"white_chocolate_donut.jpg", nil]; recipeImages = [NSArray arrayWithObjects:mainDishImages, drinkDessertImages, nil]; hearderTitleArray=@[@"分区1",@"分区2"]; [self createCollectionView]; } -(void)createCollectionView{ UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumInteritemSpacing=0.f;//item左右间隔 flowLayout.minimumLineSpacing=5.f;//item上下间隔 flowLayout.sectionInset=UIEdgeInsetsMake(5,15,5, 15);//item对象上下左右的距离 flowLayout.itemSize=CGSizeMake(80, 80);//每一个 item 对象大小 flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//设置滚动方向,默认垂直方向. flowLayout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 30);//头视图的大小 flowLayout.footerReferenceSize=CGSizeMake(self.view.frame.size.width, 30);//尾视图大小 self.rightCollectionView= [LSHControl createCollectionViewFromframe:self.view.frame collectionViewLayout:flowLayout dataSource:self delegate:self backgroudColor:[UIColor whiteColor]]; //自定义重用视图 [self.rightCollectionView registerNib:[UINib nibWithNibName:@"SHCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:SHCollectionViewCellIdetifiler]; [self.rightCollectionView registerClass:[SHCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader]; //使用原有重用视图 [self.rightCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter ]; [self.view addSubview:self.rightCollectionView]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return [recipeImages count]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [[recipeImages objectAtIndex:section] count]; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { SHCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader forIndexPath:indexPath]; [headerView getSHCollectionReusableViewHearderTitle:hearderTitleArray[indexPath.section]]; reusableview = headerView; } if (kind == UICollectionElementKindSectionFooter) { UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter forIndexPath:indexPath]; footerview.backgroundColor=[UIColor redColor]; reusableview = footerview; } return reusableview; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ SHCollectionViewCell *cell = (SHCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SHCollectionViewCellIdetifiler forIndexPath:indexPath]; UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame-2.png"]]; return cell; } @end
自定义UICollectionViewCell,使用 xib进行布局
#import@interface SHCollectionViewCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UIImageView *recipeImageView; @end #import "SHCollectionViewCell.h" @implementation SHCollectionViewCell @end
自定义UICollectionReusableView,手写代码进行布局
#import@interface SHCollectionReusableView : UICollectionReusableView -(void)getSHCollectionReusableViewHearderTitle:(NSString *)title; @end
#import "SHCollectionReusableView.h"
#import "LSHControl.h"
@interface SHCollectionReusableView (){
UILabel *titleLabel;
}
@end
@implementation SHCollectionReusableView
-(id)initWithframe:(CGRect)frame{
self=[super initWithframe:frame];
if (self) {
self.backgroundColor=[UIColor greenColor];
[self createBasicView];
}
return self;
}
-(void)createBasicView{
titleLabel=[LSHControl createLabelWithframe:CGRectMake(5, 0,self.frame.size.width-50, self.frame.size.height) Font:[UIFont systemFontOfSize:14.0] Text:@"" color:[UIColor grayColor]];
[self addSubview:titleLabel];
}
-(void)getSHCollectionReusableViewHearderTitle:(NSString *)title{
titleLabel.text=title;
}
@end
效果如下图:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



