【问题标题】:iOS7 UICollectionViewCell not being reused when dequeuing cell出队单元格时未重用iOS7 UICollectionViewCell
【发布时间】:2013-10-24 16:32:41
【问题描述】:

方法

-(void)prepareForReuse 

在我的集合视图中,从未调用单元格 - 导致我怀疑 UICollectionView 没有正确地将单元格出列。这会导致迟钝和记忆问题。

我的 collectionView 设置如下:

static NSString *cellIdentifier = @"Mycell";
-(void)initMyView
{
    [self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
            UrlLoadableCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
    if (cell.contentView.frame.size.width < 100) // tried removing this as well but didn't help
    {
        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    } else {
        cell.layer.shouldRasterize = NO;
    }
                // prepare cell
}

编辑

附加代码

static NSString *cellIdentifier = @"Mycell";

@interface UIThumbnailGalleryView
@property (nonatomic,strong) UICollectionView *collectionView;

@end


@implementation UIThumbnailGalleryView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView:frame];

    }
    return self;
}
-(void)initView:(CGRect)frame
{
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self getGalleryLayout]];

    [self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor blackColor];
    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    [self.collectionView setShowsVerticalScrollIndicator:NO];
}


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

    UrlLoadableCollectionViewCell *cell = [self.dequeueReusableCellAtIndex:indexPath];
}

-(UrlLoadableCollectionViewCell *)dequeueReusableCellAtIndex:(NSIndexPath *)index
{
    UrlLoadableCollectionViewCell *cell = (UrlLoadableCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
    return cell;
}

-(UICollectionViewFlowLayout *)getGalleryLayout
{

        UICollectionViewFlowLayout *galleryLayout = [[UICollectionViewFlowLayout alloc] init];
        [galleryLayout setItemSize:CGSizeMake(77, 77)];
        galleryLayout.minimumInteritemSpacing = 3.0;
        galleryLayout.minimumLineSpacing = 3.0;
        // iOS 6 - might need to uncomment
        //[galleryLayout setSectionInset:UIEdgeInsetsMake(44,5, 44, 5)];

    return galleryLayout;
}

【问题讨论】:

  • 很好,但在某些设备上似乎也是同样的问题
  • 您传递的是变量index 而不是indexPath。是不是打错字了?
  • 这里是一个类型,在代码中它OK :)
  • 鉴于问题发生在设备上,我认为这是一个不同的问题。

标签: ios ios7 uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

请发布包含您的 UICollectionViewCell 的 xib/场景的屏幕截图,显示它是检查器。或者,如果您的单元格完全由代码构建,请发布将您的类注册到集合视图的相关代码。通常发生这种情况是因为单元标识符中的拼写错误。

【讨论】:

  • 然后将代码发布到您在集合视图中注册自定义 UICollectionViewCell 类的位置。
  • 你有这个控制器的故事板/xib 吗?如果有一个并且它有一个具有相同标识符的单元格(可能是您开发早期遗留下来的),它几乎肯定会最后注册该标识符,因此它将是返回的实例
  • 我创建了一个非常简单的项目 - 这也不会重用单元格...github.com/avnerbarr/testApp这里有什么问题吗?
  • @AvnerBarr 该项目对我来说效果很好 - 当我滚动时,prepareForReuse 会按预期触发。听起来您的环境中的某些东西播放得不好。您可能想删除 XCode 和所有 SDK 并重新安装,可能是 beta 版遗留下来的东西。
  • 如何移除 SDK?
【解决方案2】:

你似乎没有在任何地方打电话给initMyView

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多