Reputation: 6813
I have a custom UICollectionView subclass and custom UICollectionViewCells. Problem is that When I scroll the collection view, it changes the position of the items like this:
Before scroll -
After scroll down and then back to top - (notice how Modesto and Rebecka changed postions)
The UICollectionView subclass is pretty clear :
#import "MyCustomCollectionViewController.h"
@interface MyCustomCollectionViewController ()
@end
@implementation MyCustomCollectionViewController
static NSString * const reuseIdentifier = @"cell";
-(id)initWithPins:(NSArray*)someArrayOfStuff {
self = [super initWithNibName:@"MyCustomCollectionViewController" bundle:nil];
if (self) {
if (myArray) {
myArray = [[NSMutableArray alloc] initWithArray:someArrayOfStuff];
}
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[MyCustomCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return myArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
MyCustomObject *obj = (MyCustomObject*)[myArray objectAtIndex:indexPath.row];
[cell setObject:obj];
return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize = CGSizeMake(self.view.frame.size.width/2, self.view.frame.size.width/2);
return cellSize;
}
and here is my custom UICollectionViewCell
#import "MyCustomCollectionViewCell.h"
@implementation MyCustomCollectionViewCell
@synthesize theObject;
@synthesize mediaView;
- (void)awakeFromNib {
mediaView = [[UIImageView alloc] initWithFrame:self.contentView.frame];
mediaView.backgroundColor = self.backgroundColor;
[self.contentView addSubview:mediaView];
}
-(id)initWithFrame:(CGRect)frame {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCollectionViewCell" owner:nil options:nil];
self = [topLevelObjects objectAtIndex:0];
if (self) {
self.frame = frame;
}
return self;
}
-(void)prepareForReuse {
[mediaView setMediaWithObject:theObject];
}
-(void)setObject:(MyCustomObject*)obj {
if (!theObject) {
theObject = obj;
[mediaView setMediaWithObject:obj];
}
}
@end
I'm sure there is something obvious I am missing but can't figure out what. Thanks.
Upvotes: 0
Views: 867
Reputation: 1120
I think you have to implement the false condition of
if (!theObject) {
theObject = obj;
[mediaView setMediaWithObject:obj];
} else {
[mediaView setMediaWithObject:theObject]
}
in MyCustomCollectionViewCell.m
Good Luck
Upvotes: 0
Reputation: 1122
Try this :
-(void) prepareForReuse {
[super prepareForReuse];
[mediaView setMediaWithObject:nil];
}
Upvotes: 2