0

the method

-(void)prepareForReuse 

In my collection view cell is never called - leading me to suspect that the UICollectionView is not dequeuing cells properly. This is causing lagyness and memory issues.

I've set up my collectionView as follows:

static NSString *cellIdentifier = @"Mycell";
-(void)initMyView
{
    [self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
            UrlLoadableCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
    if (cell.contentView.frame.size.width < 100) // tried removing this as well but didn't help
    {
        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    } else {
        cell.layer.shouldRasterize = NO;
    }
                // prepare cell
}

EDIT

Additional Code

static NSString *cellIdentifier = @"Mycell";

@interface UIThumbnailGalleryView
@property (nonatomic,strong) UICollectionView *collectionView;

@end


@implementation UIThumbnailGalleryView

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

    }
    return self;
}
-(void)initView:(CGRect)frame
{
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self getGalleryLayout]];

    [self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor blackColor];
    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    [self.collectionView setShowsVerticalScrollIndicator:NO];
}


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

    UrlLoadableCollectionViewCell *cell = [self.dequeueReusableCellAtIndex:indexPath];
}

-(UrlLoadableCollectionViewCell *)dequeueReusableCellAtIndex:(NSIndexPath *)index
{
    UrlLoadableCollectionViewCell *cell = (UrlLoadableCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
    return cell;
}

-(UICollectionViewFlowLayout *)getGalleryLayout
{

        UICollectionViewFlowLayout *galleryLayout = [[UICollectionViewFlowLayout alloc] init];
        [galleryLayout setItemSize:CGSizeMake(77, 77)];
        galleryLayout.minimumInteritemSpacing = 3.0;
        galleryLayout.minimumLineSpacing = 3.0;
        // iOS 6 - might need to uncomment
        //[galleryLayout setSectionInset:UIEdgeInsetsMake(44,5, 44, 5)];

    return galleryLayout;
}
Avba
  • 14,822
  • 20
  • 92
  • 192

2 Answers2

0

Please post a screenshot of the xib/scene that contains your UICollectionViewCell showing it's inspector. Or, if you're cell is constructed entirely in code, post the relevant code that registers your class with the collection view. Usually when this occurs it's because of a typo in the Cell Identifier.

RyanR
  • 7,728
  • 1
  • 25
  • 39
  • Then post the code where you're registering your custom UICollectionViewCell class with the collection view. – RyanR Oct 16 '13 at 14:39
  • Do you have a storyboard/xib for this controller? If there is one and it has a cell with the same identifier (possibly left over from earlier in your development) it will almost certainly get registered last for that identifier, and as such it will be the instance that gets returned – RyanR Oct 16 '13 at 14:52
  • I've created a very barebones project - this also doesn't reuse the cells... https://github.com/avnerbarr/testApp Any thing wrong here? – Avba Oct 16 '13 at 17:13
  • @AvnerBarr That project works fine for me - prepareForReuse fires as expected when I scroll. Sounds like something in your environment isn't playing nice. You might want to remove XCode and all the SDKs and reinstall, maybe something leftover from a beta is the cause. – RyanR Oct 16 '13 at 17:54
  • How do I remove the SDK? – Avba Oct 16 '13 at 20:22
  • strange thing, I had this problem http://stackoverflow.com/questions/18888059/cannot-find-executable-for-cfbundle-certuiframework-axbundle did the fix (completely unrelated) and now the dequeue method is being called!!?? Strange How XCode works... But still on device dequeue isn't being called... – Avba Oct 16 '13 at 21:30
  • I'm on XCode 5A1413 (the release 5.0 version). – RyanR Oct 16 '13 at 21:44
  • Same question here - http://stackoverflow.com/questions/19276509/uicollectionview-do-not-reuse-cells/19278032#19278032 – Avba Nov 06 '13 at 10:37
0

You don't seem to be calling initMyView anywhere.

Timothy Moose
  • 9,895
  • 3
  • 33
  • 44