17

我想使用 UICollectionView 在 iPhone 上的网格中显示图像,每行显示 3 张图像。对于“非常简单的测试”(如我所想),我在我的项目中添加了 15 张 JPG 图像,这样它们就会在我的包中,我可以通过 [UIImage imageNamed:...] 简单地加载它们。

我想我做的一切都是正确的(设置和注册 UICollectionViewCell 子类,使用 UICollectionViewDataSource 协议方法),但是,UICollectionView 的行为很奇怪:

它仅显示以下模式中的少数图像:第一行显示图像 1 和 3,第二行为空白,下一行与第一行相同(图像 1 和 3 正确显示),第四行空白,依此类推。 .

如果我在 NavBar 中按下触发 [self.collectionView reloadData] 的按钮,随机单元格会出现或消失。让我抓狂的是,这不仅仅是图像是否出现的问题。有时,图像也会在单元格之间交换,即它们出现在 indexPath 中,它们绝对没有连接!

这是我的单元格代码:

@interface AlbumCoverCell : UICollectionViewCell
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@end

@implementation AlbumCoverCell
@synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:frame];
        [self.contentView addSubview:_imageView];
    }
    return self;
}

- (void)dealloc
{
    [_imageView release];
    [super dealloc];
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.imageView.image = nil;
}
@end

我的 UICollectionViewController 子类的部分代码,其中“imageNames”是一个包含所有 jpg 文件名的 NSArray:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[AlbumCoverCell class] forCellWithReuseIdentifier:kAlbumCellID];
}

#pragma mark - UICollectionViewDataSource Protocol methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.imageNames count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumCoverCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kAlbumCellID forIndexPath:indexPath];
    NSString *imageName = [self.imageNames objectAtIndex:indexPath.row];
    NSLog(@"CV setting image for row %d from file in bundle with name '%@'", indexPath.row, imageName);
    cell.imageView.image = [UIImage imageNamed:imageName];

    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout Protocol methods
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    return CGSizeMake(100, 100);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

从 cellForItemAtIndexPath 中的 NSLog 语句:我可以看到所有单元格(不仅显示一个)都调用了该方法,并且 indexPath.row 和文件名之间的映射是正确的。

有谁知道什么会导致这种奇怪的行为?

4

1 回答 1

51

与此同时,我找到了解决方案。这实际上是我的UICollectionViewCell子类实现中的一个非常微妙的错误AlbumCoverCell

问题是我将单元格实例的框架设置为UIImageView子视图的框架,而不是传递单元格的边界属性contentView

这是修复:

@implementation AlbumCoverCell
@synthesize imageView = _imageView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // WRONG:
        // _imageView = [[UIImageView alloc] initWithFrame:frame];

        // RIGHT:
        _imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        [self.contentView addSubview:_imageView];
    }
    return self;
}


- (void)prepareForReuse
{
    [super prepareForReuse];

    // reset image property of imageView for reuse
    self.imageView.image = nil;

    // update frame position of subviews
    self.imageView.frame = self.contentView.bounds;
}

...

@end
于 2013-01-21T12:41:58.597 回答