Note that there are some explanatory texts on larger screens.

plurals
  1. POUICollectionView reload ( crash )
    text
    copied!<p>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 - <code>scrollViewDidEndDecelerating</code></p> <p>and the error is </p> <pre><code>-[FeedCollectionViewCell release]: message sent to deallocated instance 0x800d6f70 </code></pre> <p>here is the code. This the <code>Controller</code>:</p> <pre><code>@interface MyViewController : UIViewController &lt;UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UIScrollViewDelegate&gt; { } @property (retain, nonatomic) IBOutlet UICollectionView *collectionView; @end </code></pre> <p>This is the implementation :</p> <pre><code>@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 &gt; h - reload_distance) { NSLog(@"load more rows..."); ***//// FAIL HERE*** [self.collectionView reloadData]; } } </code></pre> <p>Here is the cell</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #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] &lt; 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 </code></pre> <p>and this is another one. adding this because its being used. I dont think the problem is here, but u never know!</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload