假设有这样的代码:
#import"UIConnectionViewDemo.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface UIConnectionViewDemo()
//数据源
@property(nonatomic,strong)NSMutableArray* dataArr;
//UIConnectionView
@property(nonatomic,strong)UICollectionView* collectView;
@property(nonatomic,assign)BOOLflag;
@end
@implementation UIConnectionViewDemo
#pragma mark -- createUI
- (void)createUI{
_dataArr= [[NSMutableArrayalloc]init];
for(inti =0; i <25; i++) {
//共有25张美女图片
[_dataArraddObject:[NSStringstringWithFormat:@"美女%02d.jpg",i+1]];
}
//NSLog(@"%@",_dataArr);//测试
}
#pragma mark --创建UICollectionView
- (void)createUICollectionView{
UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayoutalloc]init];
layout.minimumInteritemSpacing=5;
layout.minimumLineSpacing=5;
layout.itemSize=CGSizeMake(120,150);
//创建UICollectionView
_collectView= [[UICollectionViewalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)collectionViewLayout:layout];
//添加背景色
_collectView.backgroundColor= [UIColorwhiteColor];
//添加代理
_collectView.delegate=self;
_collectView.dataSource=self;
//注册cell
[_collectViewregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:@"CellID"];
//去除垂直滚动条
_collectView.showsVerticalScrollIndicator=NO;
[self.viewaddSubview:_collectView];
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorwhiteColor];
_flag=YES;
[selfcreateUI];
[selfcreateUICollectionView];
}
#pragma mark --黄金三法则,delegate方法
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section{
return_dataArr.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
UICollectionViewCell* cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"CellID"forIndexPath:indexPath];
UIImageView* imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
imageV.image= [UIImageimageNamed:_dataArr[indexPath.row]];
[cell.contentViewaddSubview:imageV];
UILabel* label = [[UILabelalloc]initWithFrame:CGRectMake(0,100,100,21)];
NSUIntegerindex = indexPath.row;
label.text= [NSStringstringWithFormat:@"第%lu张图",index];
label.textAlignment=UITextAlignmentCenter;
[cell.contentViewaddSubview:label];
return cell;
}
@end
这样运行的结果是这样的:
更改代码之前图片必须加上这样一句代码,就会好了!如下图所示!
更改代码之后图片//[注意🐷] :解决了cell上的文字重叠问题!!!!!!!!!!!!!
for(UIView * view in cell.contentView.subviews) {
[view removeFromSuperview];
}
想要完整Demo的话,可以联系我!
网友评论
eg:- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID"forIndexPath:indexPath];
UIImageView* imageV = [cell.contentView viewWithTag:10];
if (!imageV) {
imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imageV.tag = 10;
[cell.contentView addSubview:imageV];
}
imageV.image= [UIImage imageNamed:@"imageName"];
UILabel* label = [cell.contentView viewWithTag:20];
if (!label) {
label = [[UILabel alloc]initWithFrame:CGRectMake(0,100,100,21)];
label.tag = 20;
[cell.contentView addSubview:label];
}
NSUInteger index = indexPath.row;
label.text= [NSString stringWithFormat:@"第%lu张图",index];
label.textAlignment = UITextAlignmentCenter;
return cell;
}