UICollectionView中cell文字重叠的问题

假设有这样的代码:

#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

这样运行的结果是这样的:


UICollectionView中cell文字重叠的问题_第1张图片
更改代码之前图片


必须加上这样一句代码,就会好了!如下图所示!

//[注意] :解决了cell上的文字重叠问题!!!!!!!!!!!!!

for(UIView  *  view   in   cell.contentView.subviews) {

          [view   removeFromSuperview];

}


UICollectionView中cell文字重叠的问题_第2张图片
更改代码之后图片

想要完整Demo的话,可以联系我!

你可能感兴趣的:(UICollectionView中cell文字重叠的问题)