UICollectionView with dynamic cell contents - get a cell for storing state

I have UICollectionViewcontaining dynamic content. The problem that I am facing is that when a cell dequeued, it forgets about its state. In mine - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath, I have a logical meaning that, when true, changes to a true image, but when it is false, it changes to a false image. However, when I scroll down and back up, the cells forget about the state. Is there a way to make a cell remember its state? That's what is inside my

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath   

    SalesCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    SaleImage * saleImage = [self.SaleObjs objectAtIndex:indexPath.row];
    cell.bgImageView.image = [UIImage imageNamed:saleImage.imageName];
    if (saleImage.isBookable) {
        cell.isBookable = YES;
    }
    else{
        cell.isBookable=NO;
    }

return cell;
}

I have a tool for this, but it affects performance. I add this to my own cell;

-(void)prepareForReuse{
    [super prepareForReuse];


    [self setNeedsLayout];
}

Here is my collection view cell;

@implementation SalesCollectionViewCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        self.layer.cornerRadius = 6.0;
        self.bgImageView = [[UIImageView alloc] init];
        self.book = [[UILabel alloc] init];      
        [self.contentView insertSubview:self.bgImageView atIndex:0];


         return self;
}

-(void)layoutSubviews{

    [super layoutSubviews];

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

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


    }
    else{
        self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    }



}

-(void)prepareForReuse{
    [super prepareForReuse];

    [self.book removeFromSuperview];
    [self setNeedsLayout];


}
+4
6

@iphonic, isBookable (re) . UICollectionView , saleImage.isBookable , cell.isBookable, , .

:

if(saleImage.isBookable){
  self.bgImageView.frame = CGRectMake(0, 0, 140, self.bounds.size.height - self.book.bounds.size.height);
  cell.bgImageView.image = [UIImage imageNamed:saleImage.imageName];
  cell.book.hidden = NO;
}
else{
 self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
 cell.bgImageView.image = nil;
 cell.book.hidden = YES;
}

[cell layoutIfNeeded];

collectionView: cellForItemAtIndexPath:.

UILabel initWithFrame . - :

- (id)initWithFrame:(CGRect)frame{
    self.layer.cornerRadius = 6.0;
    self.bgImageView = [[UIImageView alloc] init];
    [self.contentView insertSubview:self.bgImageView atIndex:0];

    self.book = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 41, 140, 41)];      

    self.book.text = @"Book this Item";
    self.book.textColor = [UIColor whiteColor];
    self.book.adjustsFontSizeToFitWidth=YES;
    self.book.textAlignment = NSTextAlignmentCenter;
    self.book.backgroundColor= [UIColor darkGrayColor];
    self.book.font = [UIFont fontWithName:kAppFont size:17.0];
    self.book.hidden = YES;

    [self.contentView insertSubview:self.book atIndex:2];
}

layoutSubviews.

, .

+2

"" ; . cellForIndexPath .

+5
- (void)prepareForReuse {
  [super prepareForReuse];
  self.isBookable = nil;
  [self.book removeFromSuperview];
  [self setNeedsLayout];
}

isBookable . , isBookable.

+2

isBookable :

- (void) setIsBookable:(BOOL)isBookable
{
    BOOL needsLayout = isBookable != _isBookable;
    _isBookable = isBookable;
    if(needsLayout)
    {
        [self.book removeFromSuperview];
        [self setNeedsLayout];
    }
}

@property (nonatomic) BOOL isBookable; @property (nonatomic, getter = isBookable) BOOL bookable;, Apple Code.

+1

You need to create two types of cells with different identifiers:

     static NSString *Bookable = @"Bookable";
     static NSString *NonBookable = @"NonBookable";
     NSString *currentIdentifier;
     if(saleImage.isBookable)//isBookable property must be set in SaleImage class
     {
          currentIdentifier = Bookable;
     }
     else{
          currentIdentifier = NonBookable;
     }


     SalesCollectionViewCell *cell = (SalesCollectionViewCell*)[collectionView dequeueReusableCellWithIdentifier:currentIdentifier];
+1
source

1st - save / save the index cell of the cell that has been changed.

2nd -

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath   

    SalesCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    if (indexPath == self.changedIndexPath)
        [cell trueImage];
    else
        [cell falseImage];
return cell;
}

Inside a cell, you simply implement -(void) trueImageand - (void) falseImagethat change the image inside the cell.

I hope I helped :)

0
source

Source: https://habr.com/ru/post/1547139/


All Articles