0

In the UICollectionView which i use,cellForItemAtIndexPath works perfectly and displays the collectionView initially.That is collectionView.reloadData executes succesfully the first time. When it is reloaded with the following code

[self.galleryCollectionView reloadData]

with the click of a button it ends up in crash. I have enabled the Zombie objects and it shows the error

[CVCell release]: message sent to deallocated instance 0xa4f26c0.

Following is the cellForItemAtIndexPath Method

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionVieww cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"myCell";
CVCell *cell = (CVCell *)[collectionVieww dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.cellid=indexPath;
cell.delegate=self;
cell.containerImage.delegate=self;
return cell;
}

my ViewDidLoad calls the following method to create the collectionView.

-(void) createCollectionView
{

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//This is the size of a single cell courtesy:varuns research.
[flowLayout setItemSize:CGSizeMake(179, 224)];
flowLayout.minimumInteritemSpacing=45;
flowLayout.minimumLineSpacing=100;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 30, 20, 5);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.galleryCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height-120) collectionViewLayout:flowLayout];
[ self.galleryCollectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"myCell"];
[self.galleryCollectionView setDataSource:self];
[self.galleryCollectionView setDelegate:self];

self.galleryCollectionView.backgroundColor=[UIColor clearColor];
 [self.view addSubview:self.galleryCollectionView];
[self.galleryCollectionView reloadData];
}

Any workaround to suggest? Thanks in Advance

VARUN ISAC
  • 443
  • 3
  • 13

1 Answers1

1

In CreateCollectionView, do this:

CVCell *cell = [UICollectionViewCell alloc] init];
[ self.galleryCollectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"myCell"];
[self.galleryCollectionView setDataSource:self];

Then, in cellForItemAtIndexPath, this:

UICollectionViewCell *cell = [collView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

That should do it, I think. But I'm not sure what's going on where you're setting the cell.delegate, etc. I'm not sure why one would ever do that but I guess it is possible.

RegularExpression
  • 3,531
  • 2
  • 25
  • 36
  • I cant figure out the problem, but i by found that the problem occurs when the data source is empty, I bypassed the error by adding an empty invisible cell !!! – VARUN ISAC Apr 26 '14 at 16:18