AleksandarNS
AleksandarNS

Reputation: 265

UICollectionViewCell not setting properties correctly

I am trying to create custom UICollectionViewCell which contains few properties, and depending on values of those properties drawing components inside cell. I am using dequeueReusableCellWithReuseIdentifier for creating cell, then I am setting some properties, and at the end calling layoutIfNeeded function which is overridden inside my custom cell. Overridden function is setting some properties of cell also for example BOOL property is set to YES, and after refreshing cell (calling reloadData on collection view) function layoutIfNeeded is called again. When I try to read my BOOL property which is set to YES, i am always getting default value which is NO for the first time i call reloadData. When I call reloadData second time, property is set to YES. Any idea what am I doing wrong? Here is code I am using:

on button click I am calling:

[myCollectionView reloadData];

method cellForItemAtIndexPath looks like:

MyCustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCustomCell" forIndexPath: indexPath];
cell.device = [collectionArray objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
return cell;

And code of layoutIfNeeded inside MyCustomCollectionCell.m

-(void)layoutIfNeeded{
    NSLog(@"bool prop: %d",changedStatus);
    changedStatus = YES;

}

BOOL property is defined in MyCustomCollectionCell.h :

@property (nonatomic, assign)BOOL changedStatus;

UPDATE:

I am sorry, I made a mistake in my post. I am not refreshing collection with reloadData, but with reloadItemsAtIndexPaths; This call causes init method of my custom cell to be called again (not just when collection view is loaded for the first time) and after that layoutIfNeeded. I thing problem is that cell is not reused, but created again, causing all properties to disappear. Any idea how to fix this?

Upvotes: 0

Views: 520

Answers (3)

DilumN
DilumN

Reputation: 2897

Set this changedStatus Bool inside your ViewController instead of UICollectionViewCell.

BOOL property is defined in YourViewController.h :

@property (nonatomic, assign)BOOL changedStatus;

When you you want to refresh the CollectionView

[myCollectionView reloadData];
changedStatus = YES;

Then inside your MyCustomCollectionCell.m create a new method like,

-(void)customLayoutIfNeeded : (BOOL)status{
    NSLog(@"bool prop: %d",changedStatus);
    changedStatus = YES;

}

Add this to MyCustomCollectionCell.h as well.

Then inside cellForItemAtIndexPath do this,

MyCustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCustomCell" forIndexPath: indexPath];
cell.device = [collectionArray objectAtIndex:indexPath.row];
[cell customLayoutIfNeeded: changedStatus];
return cell;

Upvotes: 0

Duncan C
Duncan C

Reputation: 131398

You can't use cells to store state data. Cells get used, put in the reuse queue, and then recycled. The specific cell object that stores the data for a particular indexPath may change when the table is reloaded, when a cell is reloaded, when you scroll to expose new cells, etc.

Save state data in your data model.

Upvotes: 2

ryancrunchi
ryancrunchi

Reputation: 475

What is the purpose of changedStatus property ?

Try setting changedStatus = YES; in layoutSubviews instead :

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.changedStatus = YES;
}

Upvotes: 0

Related Questions