0
点赞
收藏
分享

微信扫一扫

[IOS]CollectionViewCell


1.CollectionView使用的是故事板,如果new 一个xib来自定义Cell,会导致故事板无法找到xib的cell

我在网上试了很多方法,但还是无法解决这个问题,最终只能直接拉一个CollectionViewCell到storyboard的View里。

 

2.具体使用方法:

一.拉一个CollectionView到storyboard中,然后再拉一个Cell到View中

二.在Identitifer中定义一个名字,如Collection_Cell

三.自定义Cell的UI,例如拉imageview和label进去

四.新建一个自定义Cell的类,然后在里面声明控件:(不要忘记建立链接)



@property (strong, nonatomic) IBOutlet UIImageView *cell_image; @property (strong, nonatomic) IBOutlet UILabel *cell_label;



 

 五.回到故事板的Cell里,更改Class指向刚才自定义的类

 

做完以上步骤则,Cell已经定义成功了,然后是view:

一.在controller.h添加



@interface HomeController : UIViewController<UICollectionViewDelegate,UICollectionViewDelegate>



 

二.在storyboard里,将CollectionView的链接管理器里添加上dataSource&delegate

三.在.m里添加一下代码:

声明view:



@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;



 

定义一个初始化数据方法:



-(void)initCellSourceData{

_cell_label_list = [NSArray arrayWithObjects:@"Devices",@"WiFi 2.4GHZ",@"WiFi 5GHZ",@"WiFi Strength"
,@"Guest Network",@"Setting",@"Iot", nil];
_cell_image_list = [NSArray arrayWithObjects:@"icobarico_4.png",@"gradico_3.png",@"gradico_3.png",@"gradico_8.png",@"gradico_6.png"
,@"gradico_2.png",@"gradico_5.png", nil];
}



  

View方法:



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.cell_label_list.count;
}

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

static NSString *cellIden = @"Collection_Cell";

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden forIndexPath:indexPath];

NSString *cell_image = self.cell_image_list[indexPath.row];
NSString *cell_label = self.cell_label_list[indexPath.row];

cell.cell_image.image = [UIImage imageNamed:cell_image];
cell.cell_label.text = cell_label;

return cell;
}



 

举报

相关推荐

0 条评论