【问题标题】:Label Overlapping in CollectionViewCollectionView 中的标签重叠
【发布时间】:2018-04-18 08:30:17
【问题描述】:

我已经使用图像和标签实现了集合视图。图像工作正常,但标签重叠。我已经实现了所有必需的方法,但找不到标签重叠的问题。下面提供了我的代码

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 40;
}
//loading data
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

   aProduct=[featuredProductsArray objectAtIndex:indexPath.row];
    UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-20, cell.frame.size.width-20)];
    //imageView.image=[UIImage imageNamed:@"ad_share.png"];
    NSString* imageUrl=[aProduct.SmallImageLists firstObject];

    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
             placeholderImage:[UIImage imageNamed:@"ad_share.png"]];

    [cell addSubview:imageView];

    UILabel *lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-60, cell.frame.size.width, 30)];
    lblTitle.text=aProduct.DealTitle;
    lblTitle.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
     lblTitle.textAlignment=NSTextAlignmentCenter;
    [cell addSubview:lblTitle];



    UILabel *lblPrice=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-30, cell.frame.size.width, 30)];
    lblPrice.text=aProduct.DealPrice;
    lblPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
     lblPrice.textAlignment=NSTextAlignmentCenter;
    [cell addSubview:lblPrice];


    cell.backgroundColor=[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:0.5];
    [cell.layer setCornerRadius:5.0f];
    [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [cell.layer setBorderWidth:0.2f];
    [cell.layer setShadowColor:[UIColor colorWithRed:225.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor];
    [cell.layer setShadowOpacity:1.0];
    [cell.layer setShadowRadius:5.0];
    [cell.layer setShadowOffset:CGSizeMake(5.0f, 5.0f)];

    return cell;
}

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width/2-10, self.view.frame.size.width/2+30);
}

截图如下

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell uicollectionreusableview


    【解决方案1】:

    问题在于细胞被重复使用。每次重复使用它们时,您都会在单元格上添加更多标签和图像。这就是为什么您会看到标签重叠的原因。

    在我看来,有两种方法可以解决它:

    • 创建UICollectionViewCell 的子类。在子类上初始化单元格时创建UIImages 和UILabels。在cellForItemAtIndexPath 上使用UIImages 和UILabels 而无需再次初始化它们。例如

      - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
      
        aProduct=[featuredProductsArray objectAtIndex:indexPath.row];
      
        NSString* imageUrl=[aProduct.SmallImageLists firstObject];
      
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
          placeholderImage:[UIImage imageNamed:@"ad_share.png"]];
      
        cell.lblTitle.text=aProduct.DealTitle;
        cell.lblTitle.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
        cell.lblTitle.textAlignment=NSTextAlignmentCenter;
      
      
        cell.lblPrice.text=aProduct.DealPrice;
        cell.lblPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
        cell.lblPrice.textAlignment=NSTextAlignmentCenter;
      
      
        cell.backgroundColor=[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:0.5];
        [cell.layer setCornerRadius:5.0f];
        [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [cell.layer setBorderWidth:0.2f];
        [cell.layer setShadowColor:[UIColor colorWithRed:225.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor];
        [cell.layer setShadowOpacity:1.0];
        [cell.layer setShadowRadius:5.0];
        [cell.layer setShadowOffset:CGSizeMake(5.0f, 5.0f)];
      
        return cell;
      }
      
    • 另一种方法是在重用时删除单元格的子视图。

      - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
      
        // Remove subviews
        for (UIView *v in cell.contentView.subviews) {
          [v removeFromSuperview];
        }
      
        aProduct=[featuredProductsArray objectAtIndex:indexPath.row];
        UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-20, cell.frame.size.width-20)];
        //imageView.image=[UIImage imageNamed:@"ad_share.png"];
        NSString* imageUrl=[aProduct.SmallImageLists firstObject];
      
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
          placeholderImage:[UIImage imageNamed:@"ad_share.png"]];
      
        [cell addSubview:imageView];
      
        UILabel *lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-60, cell.frame.size.width, 30)];
        lblTitle.text=aProduct.DealTitle;
        lblTitle.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
        lblTitle.textAlignment=NSTextAlignmentCenter;
        [cell addSubview:lblTitle];
      
      
      
        UILabel *lblPrice=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-30, cell.frame.size.width, 30)];
        lblPrice.text=aProduct.DealPrice;
        lblPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
        lblPrice.textAlignment=NSTextAlignmentCenter;
        [cell addSubview:lblPrice];
      
      
        cell.backgroundColor=[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:0.5];
        [cell.layer setCornerRadius:5.0f];
        [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [cell.layer setBorderWidth:0.2f];
        [cell.layer setShadowColor:[UIColor colorWithRed:225.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor];
        [cell.layer setShadowOpacity:1.0];
        [cell.layer setShadowRadius:5.0];
        [cell.layer setShadowOffset:CGSizeMake(5.0f, 5.0f)];
      
        return cell;
      }
      

    【讨论】:

    • 我的回答有问题吗?
    • 没有。我用 nib 文件加载了 collectionview,它根据您的第一个解决方案解决了我的问题。我厌倦了导致崩溃的第二个解决方案..
    • @MahboobiOSDeveloper 我已经更新了我的答案。当我使用cell.contentView 而不是cell.contentView.subviews 时出现一个小错别字。如果我的答案是正确的,请随时将其标记为正确答案;)
    【解决方案2】:

    你已经得到了答案。但是另一种解决方案是,因为每次您创建 UILabel 的新实例并将其添加到可重用单元格时,这就是问题的原因。使用 viewwithtag 也应该适用于您的情况。例如。

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    
    aProduct=[featuredProductsArray objectAtIndex:indexPath.row];
    
    UIImageView* imageView = [cell viewWithTag:10];
    if (imageView == nil) {
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-20, cell.frame.size.width-20)];
        imageView.tag = 10;
        [cell addSubview:imageView];
    
    }
    imageView.image=[UIImage imageNamed:@"ad_share.png"];
    NSString* imageUrl=[aProduct.SmallImageLists firstObject];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
                 placeholderImage:[UIImage imageNamed:@"ad_share.png"]];
    
    
    
    UILabel *lblTitle = [cell viewWithTag:20];
    
    if (lblTitle == nil) {
        lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-60, cell.frame.size.width, 30)];
        lblTitle.tag = 20;
        [cell addSubview:lblTitle];
    
    }
    
    lblTitle.text= aProduct.DealTitle;
    lblTitle.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
    lblTitle.textAlignment=NSTextAlignmentCenter;
    
    
    
    UILabel *lblPrice = [cell viewWithTag:30];
    
    if (lblPrice == nil) {
        lblPrice=[[UILabel alloc]initWithFrame:CGRectMake(5, cell.frame.size.height-30, cell.frame.size.width, 30)];
        lblPrice.tag = 30;
        [cell addSubview:lblPrice];
    
    }
    
    lblPrice.text= aProduct.DealPrice;
    lblPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
    lblPrice.textAlignment=NSTextAlignmentCenter;
    
    
    cell.backgroundColor=[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:0.5];
    [cell.layer setCornerRadius:5.0f];
    [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [cell.layer setBorderWidth:0.2f];
    [cell.layer setShadowColor:[UIColor colorWithRed:225.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor];
    [cell.layer setShadowOpacity:1.0];
    [cell.layer setShadowRadius:5.0];
    [cell.layer setShadowOffset:CGSizeMake(5.0f, 5.0f)];
    
    return cell;
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多