栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView

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

效果如下图:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/166922.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号