【问题标题】:UICollectionView deallocated instance errorUICollectionView 释放实例错误
【发布时间】:2014-04-19 06:10:29
【问题描述】:

在我使用的 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];
}

有什么解决方法可以建议吗? 提前致谢

【问题讨论】:

    标签: ios objective-c ipad ios6.1


    【解决方案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 等。我不确定为什么有人会这样做,但我想这是可能的。

    【讨论】:

    • 我无法弄清楚问题所在,但我发现问题发生在数据源为空时,我通过添加一个空的不可见单元格来绕过错误!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多