美文网首页iOS bug修复iOS软件开发程序员
UICollectionView中cell文字重叠的问题

UICollectionView中cell文字重叠的问题

作者: KennyHito | 来源:发表于2016-07-18 11:19 被阅读976次

    假设有这样的代码:

    #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的话,可以联系我!

    相关文章

      网友评论

      • 10399e6c5b35:如果你懒得自定义一个新的cell,你可以这样做,每次创建cell的时候去看看这个cell里有没有你需要的子控件,如果有的话直接去改。没有再创建,代码附上
        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;

        }
      • sytuzhouyong:同意楼上
      • bbc2fc74df68:这个问题你自定义cell比较好。这样做。你是在不断移除里面的控件。特别不好
        CoderLXWang:@AskeyNil 这样很正常,内部一些view的创建不会太影响性能,外部整个的cell,不会一直创建就没问题,cell变化很大的话这样做很常见
        bbc2fc74df68: @YuHaitao 如果这样做的话。复用的意义呢?
        KennyHito:@AskeyNil 如果自定义cell,肯定是最好的,但是有些地方不需要自定义的话,可以作为参考用一用,谢谢评论. :smile:

      本文标题:UICollectionView中cell文字重叠的问题

      本文链接:https://www.haomeiwen.com/subject/lcmpjttx.html