简单的UICollectionView显示图像表现奇怪:某些图像显示正确,有些图像位置错误,有些图像丢失

Taf*_*soh 17 uiimageview ios6 uicollectionview uicollectionviewcell

我想使用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
Run Code Online (Sandbox Code Playgroud)

我的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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

Taf*_*soh 50

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

问题是我将单元格实例的框架设置为UIImageView子视图的框架,而不是传递单元格的bounds属性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
Run Code Online (Sandbox Code Playgroud)

  • 我犯了类似的错误 - 感谢您发布您找到的解决方案! (4认同)
  • @Tafkadasoh谢谢!它立刻解决了我的问题,我开始撞到墙上.. (2认同)
  • @Tafkadasoh OMG!非常感谢你!解决了我的问题.这是最小的事情,但它是一个令人讨厌的bug.再次感谢发布此内容.我真的很感激. (2认同)