UICollectionView with dynamic cell content - get cell to remember state

DevC

I have a UICollectionView that contains dynamic content. The problem I am having is that when the cell is dequeued it forgets its state. In my - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath I have a boolean value that when it is true, it 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 there state. Is there a way to make a cell remember its state? Here is 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 remedy for this but it effects performance. I add this to my custom 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];


}
Don Miguel

As @iphonic said, the isBookable property should be (re)moved from the cell completely. Cells in a UICollectionView are being reused most of the time, so even though your saleImage.isBookable is in the correct state your cell.isBookable is probably not.

I would do the following:

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];

inside collectionView: cellForItemAtIndexPath:.

I would also have finished setting up the book UILabel inside initWithFrame and have it initially hidden. Something like:

- (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];
}

Then you would not need to override layoutSubviews.

Hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UICollectionView with dynamic Cell Identifier

From Dev

UICollectionView - dynamic cell height?

From Dev

Swift: UICollectionView with dynamic and static cell

From Dev

YTPlayerView inside a UICollectionView Dynamic Cell

From Dev

Swift: UICollectionView with dynamic and static cell

From Dev

Get reference to certain cell in UIcollectionview

From Dev

Get position of imageView on cell of a UICollectionView

From Dev

Get next cell in UIcollectionView in Swift

From Dev

Get cell by its content

From Dev

Get selected cell content

From Java

Dynamic cell width of UICollectionView depending on label width

From Dev

How to center a cell with the content offset in a UICollectionView with scrollToItemAtIndexPath

From Dev

how to use uibutton action in uicollectionview cell and get contents of cell

From Dev

how to use uibutton action in uicollectionview cell and get contents of cell

From Dev

UICollectionView scrollToItemAtIndexPath: and get fresh cell position

From Dev

How to get cell atIndex in UICollectionView with swift?

From Dev

How to get data from a selected UICollectionView cell?

From Dev

UICollectionView scrollToItemAtIndexPath: and get fresh cell position

From Dev

How to get data from a selected UICollectionView cell?

From Dev

QML - get tableview cell content

From Dev

Get table cell content based on content of cell in neighbourhood

From Dev

Dynamic UITableViewCell content does not expand cell

From Dev

Content hugging priority Dynamic Cell Size

From Dev

UICollectionView with dynamic height not getting same space between cell

From Dev

UICollectionView find center cell when content inset is used

From Dev

UICollectionView adding image to a cell

From Dev

Scrolling to next cell in UICollectionView

From Java

Swift UICollectionView Cell not stretched

From Dev

Image fits into UICollectionView Cell

Related Related

  1. 1

    UICollectionView with dynamic Cell Identifier

  2. 2

    UICollectionView - dynamic cell height?

  3. 3

    Swift: UICollectionView with dynamic and static cell

  4. 4

    YTPlayerView inside a UICollectionView Dynamic Cell

  5. 5

    Swift: UICollectionView with dynamic and static cell

  6. 6

    Get reference to certain cell in UIcollectionview

  7. 7

    Get position of imageView on cell of a UICollectionView

  8. 8

    Get next cell in UIcollectionView in Swift

  9. 9

    Get cell by its content

  10. 10

    Get selected cell content

  11. 11

    Dynamic cell width of UICollectionView depending on label width

  12. 12

    How to center a cell with the content offset in a UICollectionView with scrollToItemAtIndexPath

  13. 13

    how to use uibutton action in uicollectionview cell and get contents of cell

  14. 14

    how to use uibutton action in uicollectionview cell and get contents of cell

  15. 15

    UICollectionView scrollToItemAtIndexPath: and get fresh cell position

  16. 16

    How to get cell atIndex in UICollectionView with swift?

  17. 17

    How to get data from a selected UICollectionView cell?

  18. 18

    UICollectionView scrollToItemAtIndexPath: and get fresh cell position

  19. 19

    How to get data from a selected UICollectionView cell?

  20. 20

    QML - get tableview cell content

  21. 21

    Get table cell content based on content of cell in neighbourhood

  22. 22

    Dynamic UITableViewCell content does not expand cell

  23. 23

    Content hugging priority Dynamic Cell Size

  24. 24

    UICollectionView with dynamic height not getting same space between cell

  25. 25

    UICollectionView find center cell when content inset is used

  26. 26

    UICollectionView adding image to a cell

  27. 27

    Scrolling to next cell in UICollectionView

  28. 28

    Swift UICollectionView Cell not stretched

  29. 29

    Image fits into UICollectionView Cell

HotTag

Archive