自定义集合视图单元格

时间:2015-10-16 11:32:42

标签: ios objective-c

EDIT: I have added a screenshot of the problem

我使用了Custom Collection View Cell。问题是当我快速水平滚动列表时,我在一些单元格的图像视图中得到2个图像。 请帮助我理解为什么会发生这种情况以及如何纠正它?

这是集合视图代码 -

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  //  static NSString * const reuseIdentifier = @"cell3";
    MostPopularViewCell *cell3 = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];



         cell3.clipsToBounds=YES;
            cell3.mostproductprice.text=[[mostPopularArr objectAtIndex:indexPath.row] objectForKey:@"name"];
            cell3.mostproductnm.text=[[mostPopularArr objectAtIndex:indexPath.row] objectForKey:@"pirce"];


            NSDictionary *Scrapdict=[mostPopularArr objectAtIndex:indexPath.row];
                     NSString *img_str=[Scrapdict objectForKey:@"small_image"];
            NSURL *url=[NSURL URLWithString:img_str];
            AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:cell3.mostProductimg.bounds];
            imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


            [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
            //set image URL. AsyncImageView class will then dynamically load the image
            ((AsyncImageView *)imageView).imageURL =url;


          //  imageView.clipsToBounds=YES;
            imageView.userInteractionEnabled=YES;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.userInteractionEnabled=YES;
            cell3.backgroundColor=[UIColor clearColor];
            cell3.mostProductimg.image=nil;

                   //imageView.layer.cornerRadius = 10;
            imageView.backgroundColor=[UIColor clearColor];
            //cell3.mostProductimg.clipsToBounds=YES;

            [cell3.mostProductimg  addSubview:imageView];
            cell3.mostProductimg.userInteractionEnabled=YES;
            [AsyncImageLoader defaultCache];

            [[AsyncImageLoader sharedLoader]loadImageWithURL:url target:nil success:nil failure:nil];

           // cell3.mostProductimg.clipsToBounds=YES;

            cell3.mostPopularviewbtn.tag = indexPath.row;
            [cell3.mostPopularviewbtn addTarget:self action:@selector(btnClick3:) forControlEvents:UIControlEventTouchUpInside];



    // Configure the cell


    return cell3;
}

集合视图单元代码 -

- (void)awakeFromNib {

}
- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MostPopularViewCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];




    }

    return self;

}

1 个答案:

答案 0 :(得分:0)

[cell3.mostProductimg addSubview:imageView];为您的图片提供标记之前,从您的自定义单元格中删除具有相同标记的子视图。所以你可以用

替换[cell3.mostProductimg addSubview:imageView];
imageView.tag=123;
UIView *viewToRemove = [cell3.mostProductimg viewWithTag:123];
if(viewToRemove !=nil)
   [viewToRemove removeFromSuperview];
[cell3.mostProductimg  addSubview:imageView];

这应该有效。 您的代码的问题是您每次都在重用单元格并添加imageView。您可以在自定义单元格的xib中添加imageView并为其创建一个插座。之后,您可以使用插座更改其图像,而不是在其上添加新的imageView。 希望这会有所帮助。