UICollectionView解除分配实例错误

时间:2014-04-19 06:10:29

标签: ios objective-c ipad ios6.1

在我使用的UICollectionView中,cellForItemAtIndexPath工作正常并最初显示collectionView。这是collectionView.reloadData第一次成功执行。 当使用以下代码重新加载

[self.galleryCollectionView reloadData]

点击一个按钮就会崩溃。我启用了Zombie对象并显示错误

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

以下是cellForItemAtIndexPath方法

-(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;
}

我的ViewDidLoad调用以下方法来创建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];
}

建议的任何解决方法? 在此先感谢

1 个答案:

答案 0 :(得分:1)

在CreateCollectionView中,执行以下操作:

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

然后,在cellForItemAtIndexPath中,这个:

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

我认为应该这样做。但我不确定你在哪里设置cell.delegate等等。我不确定为什么会这样做但我想这是可能的。< / p>