先看纯代码创建自定义单元格
#import<UIKit / UIKit.h>
@interfaceCustomCollectionviewCellCollectionViewCell:UICollectionViewCell
- (void)changeLabelBackgroundColor;
- (void)setTabelText :( NSString *)text;
@end
#import“CustomCollectionviewCellCollectionViewCell.h”
@interface CustomCollectionviewCellCheckViewCell()
@property(nonatomic ,strong)UILabel * label;
@property(nonatomic,strong)UIImageView* imageView;
@end
@implementation CustomCollectionviewCellCollectionViewCell
- (void)layoutSubviews {
[ super layoutSubviews ];
self.imageView= [[ UIImageViewalloc] initWithImage :[UIImageimageNamed:@“backgroundimage.png” ]];
self.imageView.frame = CGRectMake(30.0f ,30.0f,self.frame.size.width-60.0f ,self.frame.size.height-60.0f );
[ self.contentViewaddSubview:self.imageView];
self.label= [[ UILabelALLOC] initWithFrame:CGRectMake(30.0f,self.ImageView.frame.size.height+30.0f , self.ImageView.frame.size.width-60.0f ,self.frame.size.width-self.ImageView.frame.size.height-30.0f )];
self.label.textColor= [ UIColor whiteColor];
self.label.textAlignment= NSTextAlignmentCenter
self.label.font= [ UIFontsystemFontOfSize:14.0f];
[ self.contentView addSubview:self.label];
}
- (void)changeLabelBackgroundColor {
self.label.backgroundColor= [ UIColor blueColor];
}
- (void)setTabelText :( NSString *)text {
self.label.text= text;
}
@end
#import“CustomCollectionView.h”
#import“CustomCollectionviewCellCollectionViewCell.h”
#define COLLECTIONVIEW_CELL_IDENTIFIER @“Cell”
@interface CustomCollectionView()
@property(nonatomic,strong)IBOutletUICollectionView* collectionView;
@end
@implementationCustomCollectionView
- (void)viewDidLoad {
[ super viewDidLoad ];
//加载视图后再执行其他设置。
[ self.collectionView registerClass:[ CustomCollectionviewCellCollectionViewCellclass] forCellWithReuseIdentifier :COLLECTIONVIEW_CELL_IDENTIFIER ];
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}
- (NSInteger)collectionView :( UICollectionView*)collectionView numberOfItemsInSection :( NSInteger)section {
return 10 ;
}
- (NSInteger)numberOfSectionsInCollectionView :( UICollectionView*)collectionView {
return 1 ;
}
- (CustomCollectionviewCellCollectionViewCell*)CollectionView :( UICollectionView*)viewCollection cellForItemAtIndexPath :( NSIndexPath*)indexPath {
CustomCollectionviewCellCollectionViewCell * cell = [viewCollectiondequeueReusableCellWithReuseIdentifier :COLLECTIONVIEW_CELL_IDENTIFIERforIndexPath:indexPath];
[celll ayoutIfNeeded ];//这里一定要加上这一句,迫使布局提前进行,否则在实际的调用过程中,layoutsubview总是会最后才执行到,导致标签文字等显示不出来。
[cellcha ngeLabelBackgroundColor ];
[cellsetTabelText :[ NSStringstringWithFormat:@“%@%ld” ,@“Label”,indexPath.row]];
return cell; }
也可以从nib文件来实现cell
故事板中已经有了collectionview
创建好了对应的viewcontroller并且提供了数据源方法
3.工程中新文件 - >空保存的时候后缀保存成.xib的形式
4.选择collectionveiwcell并且拖放至xib中
5.添加想添加的控件
关键的地方是在注册单元的时候更改为加载nib文件的形式
具体代码如下:
#import“CustomCollectionView.h”
#import“CustomCollectionviewCellCollectionViewCell.h”
#define COLLECTIONVIEW_CELL_IDENTIFIER @“Cell”
@interfaceCustomCollectionView ()
@property(nonatomic ,strong)IBOutlet UICollectionView* collectionView;
@end
@implementationCustomCollectionView
-(void)viewDidLoad {
[super viewDidLoad];
//加载视图后再执行其他设置。
//[self.collectionView registerClass:[CustomCollectionviewCellCollectionViewCell class] forCellWithReuseIdentifier:COLLECTIONVIEW_CELL_IDENTIFIER];
//从xib文件中注册
[ self。collectionViewregisterNib:[ UINibnibWithNibName :@“CustomCell”bundle :[NSBundle mainBundle]] forCellWithReuseIdentifier:COLLECTIONVIEW_CELL_IDENTIFIER]; //注册的时候以nib文件的形式加载
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
//处理可以重新创建的任何资源。
}
- (NSInteger)collectionView :( UICollectionView*)collectionView numberOfItemsInSection :( NSInteger)section {
返回10 ;
}
- (NSInteger)numberOfSectionsInCollectionView :( UICollectionView*)collectionView {
return 1 ;
}
- (UICollectionViewCell*)的CollectionView :( UICollectionView*)viewCollection cellForItemAtIndexPath :( 非空NSIndexPath*)indexPath {
UICollectionViewCell * cell = [viewCollectiondequeueReusableCellWithReuseIdentifier:COLLECTIONVIEW_CELL_IDENTIFIERforIndexPath:indexPath];
返回单元格
}
@end