-2

I have a collections view and the view works fine when I load the data initially, but crashes when I try to reload it. take a look at the reload thats happening on the method - scrollViewDidEndDecelerating

and the error is

-[FeedCollectionViewCell release]: message sent to deallocated instance 0x800d6f70

here is the code. This the Controller:

@interface MyViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
{

}

@property (retain, nonatomic) IBOutlet UICollectionView *collectionView;

@end

This is the implementation :

@implementation MyViewController

@interface FeedCollectionViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
-(void)getData;
@end

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        if (self.dataArray == nil) {
            self.dataArray = [[NSMutableArray alloc] init];
        }
    }
    return self;
}

- (void)viewDidLoad
{

[self.collectionView registerClass:[FeedCollectionViewCell class] forCellWithReuseIdentifier:[FeedCollectionViewCell reuseIdentifier]];


    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(153, 128)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:flowLayout];

    [self getData];


}

-(void)getData
{
 // here I make a call to the server to get the data and I set the data array
   self.dataArray = mydatafromserver
   [self.collectionView reloadData];
}

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

    FeedCollectionViewCell *cell = (FeedCollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:[FeedCollectionViewCell reuseIdentifier] forIndexPath:indexPath];

    [cell.label setText:[[self.dataArray objectAtIndex:indexPath.row] name];

    return cell;
}

- (void)scrollViewDidEndDecelerating:(UICollectionView *) collectionView
{
    NSLog(@"FeedCollectionViewController::scrollViewDidEndDecelerating");
    CGPoint offset = collectionView.contentOffset;
    CGRect bounds = collectionView.bounds;
    CGSize size = collectionView.contentSize;
    UIEdgeInsets inset = collectionView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

      //   NSLog(@"offset: %f", offset.y);
      //   NSLog(@"content.height: %f", size.height);
      //   NSLog(@"bounds.height: %f", bounds.size.height);
      //   NSLog(@"inset.top: %f", inset.top);
      //   NSLog(@"inset.bottom: %f", inset.bottom);
      //   NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 300;
    if(y > h - reload_distance) {
        NSLog(@"load more rows...");

***//// FAIL HERE***       
        [self.collectionView reloadData];

    }
}

Here is the cell

#import <UIKit/UIKit.h>
#define FB_COLL_CELL_IDENTIFIER     @"CollectionCellIdentifier"

@interface FeedCollectionViewCell : UICollectionViewCell


@property (retain, nonatomic) IBOutlet UIImageView *image;
@property (retain, nonatomic) IBOutlet UILabel *label;

@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
+ (NSString *)reuseIdentifier;

@end

#import "FeedCollectionViewCell.h"
#import "CustomCellBackground.h"

@implementation FeedCollectionViewCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"FeedCollectionViewCell initWithFrame");
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"FeedCollectionViewCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];

        CustomCellBackground *backgroundView = [[CustomCellBackground alloc] initWithFrame:CGRectZero];
        self.selectedBackgroundView = backgroundView;

    }

    return self;

}


+ (NSString *)reuseIdentifier
{
    NSLog(@"FBDisplayCell ... static reuseIdentifier called ");
    return (NSString *)FB_COLL_CELL_IDENTIFIER;
}


@end

and this is another one. adding this because its being used. I dont think the problem is here, but u never know!

#import <UIKit/UIKit.h>

@interface CustomCellBackground : UIView

@end

#import "CustomCellBackground.h"

@implementation CustomCellBackground

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // draw a rounded rect bezier path filled with blue
    CGContextRef aRef = UIGraphicsGetCurrentContext();
    CGContextSaveGState(aRef);
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5.0f];
    [bezierPath setLineWidth:5.0f];
    [[UIColor blackColor] setStroke];

    UIColor *fillColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]; // color equivalent is #87ceeb
    [fillColor setFill];

    [bezierPath stroke];
    [bezierPath fill];
    CGContextRestoreGState(aRef);
}

@end
etolstoy
  • 1,798
  • 21
  • 33
verma
  • 753
  • 8
  • 8
  • You are using `UICollectionView` in a project without ARC. Is that allowed by the framework? – Sergey Grischyov Sep 06 '13 at 12:37
  • Is there a good way to find that out? – verma Sep 06 '13 at 12:57
  • Don't know, have been using `ARC` from the start. It's been around for ages. You're calling `[FeedCollectionViewCell release]` somewhere when it was already released, that's the problem. – Sergey Grischyov Sep 06 '13 at 13:05
  • I checked my compiler, and I am using this compiler : Apple LLVM compiler 4.2 – verma Sep 06 '13 at 13:18
  • and, about the release. thats where my confusion is, I dont have the feedCollectionViewCell being release. I dont have code thats doing the release. – verma Sep 06 '13 at 13:20

1 Answers1

-1

The issue was with the UICollectioViewCell -initWithFrame

i changed to not load the nib and i started creating my own

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.85f alpha:1.0f];

        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.layer.borderWidth = 3.0f;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowRadius = 3.0f;
        self.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
        self.layer.shadowOpacity = 0.5f;

        self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageView.clipsToBounds = YES;

        [self.contentView addSubview:self.imageView];
    }
    return self;
    }
verma
  • 753
  • 8
  • 8