简体   繁体   English

滚动后,自定义UICollectionView单元格layoutsubview不正确

[英]Custom UICollectionView cells layoutsubview is incorrect after scrolling

I have a custom UICollectionViewCell that contains a BOOL value called isOnSale. 我有一个自定义UICollectionViewCell ,其中包含一个名为isOnSale的BOOL值。 I set this value in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath . 我在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath设置此值。 A label is added to the cell if the isOnSale is true. 如果isOnSale为true, isOnSale标签添加到单元格。 This works perfectly, however if I scroll down and then scroll back up the labels have swapped to the cell next in the index. 这可以完美地工作,但是如果我向下滚动然后向上滚动,标签将交换到索引中的下一个单元格。 I scroll up, and back down, they are back to there original places. 我向上滚动,然后向下滚动,它们又回到了原始位置。 I am guessing it is a layout problem but I have spent the last hour or so trying to spot the bug, while implementing other examples here. 我猜这是一个布局问题,但是我花了最后一个小时左右的时间来尝试发现错误,同时在这里实现其他示例。

Here is my UICollectionViewCell 这是我的UICollectionViewCell

@implementation SaleCollectionViewCell


- (id)initWithFrame:(CGRect)frame
    {
       self = [super initWithFrame:frame];
     if (self) {
        // Initialization code
        self.bgImageView = [[UIImageView alloc] init];
        self.sale = [[UILabel alloc] init];
        self.bgImageView.image = [UIImage imageNamed:@"iron1"];

        [self.contentView insertSubview:self.bgImageView atIndex:0];

        self.layer.cornerRadius = 6.0;

    }
    return self;
}

-(void)layoutSubviews{

    [super layoutSubviews];

    if (self.isOnSale) {
        self.sale.frame =CGRectMake(0, self.bounds.size.height - 41, 140, 41);
        self.sale.text = @"On Sale";
        self.sale.textColor = [UIColor whiteColor];
        self.sale.adjustsFontSizeToFitWidth=YES;
        self.sale.textAlignment = NSTextAlignmentCenter;
        self.sale.backgroundColor= [UIColor darkGrayColor];
        self.sale.font = [UIFont fontWithName:kAppFont size:17.0];
        [self.contentView insertSubview:self.sale atIndex:2];

        self.bgImageView.frame =CGRectMake(0, 0, 140, self.bounds.size.height - self.sale.bounds.size.height);

    }
    else{
        NSLog(@"Is not on sale");
        self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);


    }

}
@end

If the item is on sale then layoutSubViews updates the view and adds the Sale label to the cell. 如果商品正在销售中,则layoutSubViews更新视图并将“ Sale标签添加到单元格中。

Here is my UICollectionView - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 这是我的UICollectionView - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SaleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.clipsToBounds = YES;

    SaleImage * saleImage = [self.saleObjs objectAtIndex:indexPath.row];
    cell.bgImageView.image = [UIImage imageNamed:saleImage.imageName];
    if (saleImage.isOnSale) {
        cell.isOnSale = YES;
    }
    else{
        cell.isOnSale=NO;
    }

    return cell;

}

The problem is that the Sale label is moving when the cells leave the screen. 问题是当单元格离开屏幕时,销售标签正在移动。 Its strange, because the self.bgImageView shows the correct content. 这很奇怪,因为self.bgImageView显示正确的内容。

If anything is unclear please ask. 如果不清楚,请询问。

layoutSubviewselse分支或-prepareForResuse方法中从-prepareForResuse视图中删除标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM