Collectionview学习(四)不使用故事版,纯代码编写cell(cell也可从nib文件加载)

先看纯代码创建自定义单元格

#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] initWithFrameCGRectMake30.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= [ UIFontsystemFontOfSize14.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文件中注册

    [ selfcollectionViewregisterNib:[ UINibnibWithNibName @“CustomCell”bundle :[NSBundle mainBundle]]  forCellWithReuseIdentifierCOLLECTIONVIEW_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 = [viewCollectiondequeueReusableCellWithReuseIdentifierCOLLECTIONVIEW_CELL_IDENTIFIERforIndexPathindexPath];

    返回单元格

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值