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

2020-01-18 19:43:50王冬梅

自定义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

效果如下图:

IOS,自定义UICollectionView,自定义UICollectionView实例详解,UICollectionViewIOS,自定义UICollectionView,自定义UICollectionView实例详解,UICollectionView

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


注:相关教程知识阅读请移步到IOS开发频道。