最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年01月06日 11:01:11   作者:Jack__Long  
這篇文章主要介紹了IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView的相關(guān)資料,需要的朋友可以參考下

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

其實(shí)看標(biāo)題就知道是需要繼承于UICollectionReusableView,實(shí)現(xiàn)一個(gè)滿足自己需求的視圖.那么如何操作了,看下面代碼:

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=@[@"分區(qū)1",@"分區(qū)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對(duì)象上下左右的距離
  flowLayout.itemSize=CGSizeMake(80, 80);//每一個(gè) item 對(duì)象大小
  flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//設(shè)置滾動(dòng)方向,默認(rèn)垂直方向.
  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方法,但是在獲取重用視圖的時(shí)候,系統(tǒng)會(huì)自動(dòng)調(diào)用initWithFrame方法的.所以在initWithFrame里面進(jìn)行初始化操作,是沒有問題的!
     */

    [headerView getSHCollectionReusableViewHearderTitle:hearderTitleArray[indexPath.section]];

    reusableview = headerView;
  }

  if (kind == UICollectionElementKindSectionFooter) {

    UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter forIndexPath:indexPath];
    /**
     *  如果頭尾視圖沒什么很多內(nèi)容,直接創(chuàng)建對(duì)應(yīng)控件進(jìn)行添加即可,無需自定義.
     */
    footerview.backgroundColor=[UIColor redColor];
    reusableview = footerview;
  }

  return reusableview;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

  /**
   *
   *  這里自定義cell,使用xib進(jìn)行布局.對(duì)于如何使用xib創(chuàng)建視圖,不明白的,可以看我之前寫的一篇文章.
   */


  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進(jìn)行布局

#import <UIKit/UIKit.h>

@interface SHCollectionViewCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *recipeImageView;

@end

#import "SHCollectionViewCell.h"
@implementation SHCollectionViewCell


@end

自定義UICollectionReusableView,手寫代碼進(jìn)行布局

#import <UIKit/UIKit.h>

@interface SHCollectionReusableView : UICollectionReusableView

/**
 *  聲明相應(yīng)的數(shù)據(jù)模型屬性,進(jìn)行賦值操作,獲取頭視圖或尾視圖需要的數(shù)據(jù).或者提供一個(gè)方法獲取需要的數(shù)據(jù).
 */

-(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;

}

/**
 *  進(jìn)行基本布局操作,根據(jù)需求進(jìn)行.
 */
-(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];


}


/**
 *  設(shè)置相應(yīng)的數(shù)據(jù)
 *
 *  @param title 
 */
-(void)getSHCollectionReusableViewHearderTitle:(NSString *)title{

  titleLabel.text=title;

}

@end

效果如下圖:

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

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

相關(guān)文章

  • 詳解iOS App開發(fā)中Cookie的管理方法

    詳解iOS App開發(fā)中Cookie的管理方法

    iOS中主要靠NSHTTPCookieStorage和NSHTTPCookie來管理Cookie,下面我們就來詳解iOS App開發(fā)中Cookie的管理方法,在最后一部分會(huì)單獨(dú)整理出如何清除Cookie的方法.
    2016-07-07
  • iOS10語音識(shí)別框架SpeechFramework應(yīng)用詳解

    iOS10語音識(shí)別框架SpeechFramework應(yīng)用詳解

    在iOS10系統(tǒng)了,apple開放了與語音識(shí)別相關(guān)的接口,開發(fā)者可以將其應(yīng)用到自己的App中,實(shí)現(xiàn)用戶通過語音進(jìn)行功能操作。 這篇文章主要介紹了iOS10語音識(shí)別框架SpeechFramework應(yīng)用,需要的朋友可以參考下
    2016-09-09
  • iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法

    iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法

    UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實(shí)現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:
    2016-07-07
  • XCode編譯速度慢的處理方法

    XCode編譯速度慢的處理方法

    本文給大家介紹的是在IOS開發(fā)中XCode編譯速度慢的3種解決辦法,十分的實(shí)用,有需要的小伙伴可以參考下。
    2015-06-06
  • iOS源碼閱讀必備知識(shí)之Tagged Pointer

    iOS源碼閱讀必備知識(shí)之Tagged Pointer

    這篇文章主要給大家介紹了關(guān)于iOS源碼閱讀必備知識(shí)之Tagged Pointer的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB)

    iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB)

    這篇文章主要介紹了iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • iOS開發(fā)輸入自動(dòng)填充UITextField背景色

    iOS開發(fā)輸入自動(dòng)填充UITextField背景色

    如何在iOS中實(shí)現(xiàn)輸入時(shí)自動(dòng)填充背景色的效果,首先,我們設(shè)置UITextField的背景色為初始顏色,然后,通過設(shè)置UITextField的代理,并監(jiān)聽UITextField的輸入事件,我們在用戶開始輸入時(shí)將其背景色改變?yōu)楦吡令伾?在用戶結(jié)束輸入時(shí)恢復(fù)為初始顏色
    2023-10-10
  • iOS體驗(yàn)性優(yōu)化之RTL適配右滑返回的實(shí)現(xiàn)

    iOS體驗(yàn)性優(yōu)化之RTL適配右滑返回的實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于iOS體驗(yàn)性優(yōu)化之RTL適配右滑返回實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • iOS UIAlertController中UITextField添加晃動(dòng)效果與邊框顏色詳解

    iOS UIAlertController中UITextField添加晃動(dòng)效果與邊框顏色詳解

    這篇文章主要給大家介紹了關(guān)于iOS UIAlertController中UITextField添加晃動(dòng)效果與邊框顏色的相關(guān)資料,實(shí)現(xiàn)后的效果非常適合在開發(fā)中使用,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2017-10-10
  • 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    這篇文章主要為大家詳細(xì)介紹了iOS開關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評(píng)論

阳江市| 菏泽市| 江山市| 吉木萨尔县| 息烽县| 锡林郭勒盟| 佛教| 连南| 都江堰市| 合作市| 积石山| 涿州市| 元谋县| 蒙山县| 神农架林区| 彭泽县| 嘉峪关市| 扎鲁特旗| 墨江| 修武县| 禄丰县| 石柱| 安远县| 扶绥县| 临沭县| 东辽县| 莲花县| 高州市| 北碚区| 芜湖市| 缙云县| 恭城| 西吉县| 会泽县| 米易县| 乌什县| 乌鲁木齐县| 阳城县| 毕节市| 敦煌市| 龙岩市|