亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView

2020-07-26 02:59:26
字體:
來源:轉載
供稿:網友

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 ()<UICollectionViewDelegate,UICollectionViewDataSource>{  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];    /**     *     * 注意:雖然這里沒有看到明顯的initWithFrame方法,但是在獲取重用視圖的時候,系統會自動調用initWithFrame方法的.所以在initWithFrame里面進行初始化操作,是沒有問題的!     */    [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{  /**   *   *  這里自定義cell,使用xib進行布局.對于如何使用xib創建視圖,不明白的,可以看我之前寫的一篇文章.   */  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 <UIKit/UIKit.h>@interface SHCollectionViewCell : UICollectionViewCell@property (weak, nonatomic) IBOutlet UIImageView *recipeImageView;@end#import "SHCollectionViewCell.h"@implementation SHCollectionViewCell@end

自定義UICollectionReusableView,手寫代碼進行布局

#import <UIKit/UIKit.h>@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];}/** *  設置相應的數據 * *  @param title  */-(void)getSHCollectionReusableViewHearderTitle:(NSString *)title{  titleLabel.text=title;}@end

效果如下圖:

這里寫圖片描述這里寫圖片描述

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日本精品一区二区三区在线播放视频| 国内外成人免费激情在线视频网站| 亚洲www永久成人夜色| 国外成人在线视频| 国产精品va在线播放| 中文字幕av一区| 久青草国产97香蕉在线视频| 亚洲欧洲视频在线| 久久久这里只有精品视频| 成人免费看吃奶视频网站| 久久人人爽人人爽人人片av高清| 久久久久久69| 51精品国产黑色丝袜高跟鞋| 91九色视频导航| 国产精品人成电影在线观看| 成人黄色av播放免费| 日韩a**中文字幕| 国产精品高清在线| 海角国产乱辈乱精品视频| 国产福利精品av综合导导航| 亚洲在线免费视频| 日韩中文字幕在线看| 国内成人精品一区| 黄色成人av网| 精品国产一区二区三区久久久狼| 亚洲欧美国产一本综合首页| 91夜夜揉人人捏人人添红杏| 亚洲欧洲一区二区三区在线观看| 午夜欧美不卡精品aaaaa| 国产成人黄色av| 国模精品一区二区三区色天香| 精品久久久久久久久国产字幕| 欧美午夜女人视频在线| 国产精品成人观看视频国产奇米| 国产亚洲激情视频在线| 久久久久九九九九| 精品久久久久久亚洲国产300| 97在线视频免费播放| 萌白酱国产一区二区| 成人国产精品久久久| 国产在线观看一区二区三区| 午夜免费久久久久| 久久久久久久一| 中文字幕亚洲综合| 欧美综合一区第一页| 亚洲国产成人91精品| 久久综合免费视频| 中文字幕日韩av电影| 不卡在线观看电视剧完整版| 久久久伊人日本| 欧美国产日产韩国视频| 庆余年2免费日韩剧观看大牛| 精品中文字幕久久久久久| 亚洲精品一区久久久久久| 精品国产乱码久久久久酒店| 欧美裸体xxxx极品少妇软件| 欧美风情在线观看| zzijzzij亚洲日本成熟少妇| 成人在线激情视频| 另类色图亚洲色图| 成人做爰www免费看视频网站| 色久欧美在线视频观看| 中日韩美女免费视频网站在线观看| 亚洲国产免费av| 亚洲欧美日韩爽爽影院| 国产精品高潮呻吟久久av野狼| 亚洲天堂网站在线观看视频| 精品国产福利视频| 欧美性猛交xxxxx水多| 国产精品日本精品| 一道本无吗dⅴd在线播放一区| 久久精品精品电影网| 少妇高潮久久久久久潘金莲| 久久视频国产精品免费视频在线| 日韩69视频在线观看| 亚洲精品电影久久久| 奇门遁甲1982国语版免费观看高清| 丝袜美腿亚洲一区二区| 亚洲乱码一区av黑人高潮| 国产精品久久久久免费a∨| 九九精品在线观看| 国产精品视频1区| 国产日韩中文字幕| 久久九九免费视频| 中文字幕一精品亚洲无线一区| 96sao精品视频在线观看| 夜夜嗨av一区二区三区四区| 亚洲性线免费观看视频成熟| 久久亚洲春色中文字幕| 一本色道久久综合狠狠躁篇怎么玩| 国产成人精品久久亚洲高清不卡| 国产视频精品在线| 国产成人精品在线| 精品伊人久久97| 欧美丰满少妇xxxx| 亚洲在线视频观看| 日本一区二区三区在线播放| 欧美乱大交xxxxx另类电影| 曰本色欧美视频在线| 亚洲精品视频免费| 国产亚洲a∨片在线观看| 欧美黑人巨大精品一区二区| 狠狠久久亚洲欧美专区| 欧美激情一区二区三区在线视频观看| 欧美高清在线视频观看不卡| 成人在线免费观看视视频| 国产精品一区二区三区在线播放| 国产专区欧美专区| 久久精品99久久香蕉国产色戒| 在线免费观看羞羞视频一区二区| 欧美精品在线第一页| 国产精品人人做人人爽| 亚洲xxxx视频| 日韩欧美a级成人黄色| 欧美国产日韩一区二区| 日本精品一区二区三区在线播放视频| 欧美一级淫片aaaaaaa视频| 国产成人极品视频| 成人欧美在线视频| 97久久精品人人澡人人爽缅北| 亚洲精品mp4| 精品视频偷偷看在线观看| 国内精品在线一区| 91在线无精精品一区二区| 国产日韩在线免费| 国产日韩欧美日韩| 国产精品久久一区主播| 中文国产成人精品久久一| 欧美一区在线直播| 色小说视频一区| 国产免费亚洲高清| 欧美激情图片区| 91精品国产综合久久香蕉922| 91精品国产自产在线| 成人欧美一区二区三区黑人孕妇| 国产精品美女午夜av| 永久免费精品影视网站| 一区二区欧美久久| 欧美亚洲免费电影| 91国产高清在线| 欧美黄网免费在线观看| 久久久久久91香蕉国产| 亚洲午夜精品久久久久久久久久久久| 97欧美精品一区二区三区| 久久久久久久国产精品视频| 性色av一区二区三区免费| 亚洲美腿欧美激情另类| 91在线无精精品一区二区| 97精品欧美一区二区三区| 国产精品88a∨| 欧美激情视频一区二区| 国产精品扒开腿做爽爽爽男男| 日韩最新中文字幕电影免费看| 国产精品久久综合av爱欲tv| 欧美亚洲在线播放| 51ⅴ精品国产91久久久久久| 日韩一级裸体免费视频| 精品综合久久久久久97| 日本韩国欧美精品大片卡二| 欧美精品第一页在线播放| 亚洲国产高清高潮精品美女| 国产+人+亚洲| 色偷偷综合社区| 亚洲精品98久久久久久中文字幕|