首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >显示图像的简单UICollectionView的行为很奇怪:一些图像显示正确,一些图像显示在错误的位置,有些图像根本就不存在

显示图像的简单UICollectionView的行为很奇怪:一些图像显示正确,一些图像显示在错误的位置,有些图像根本就不存在
EN

Stack Overflow用户
提问于 2012-12-06 09:11:58
回答 1查看 18.7K关注 0票数 17

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

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

它只显示了以下模式中的几个图像:第一行显示图像1和3,第二行是空白的,下一行与第一行相似(图像1和3显示正确),第四行空白,依此类推……

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

下面是我的单元格代码:

代码语言:javascript
复制
@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

我的jpg子类的部分代码,其中'imageNames‘是一个保存所有UICollectionViewController文件名的NSArray:

代码语言:javascript
复制
- (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和文件名之间的映射是正确的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-21 20:41:59

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

问题是,我已经将单元实例的框架设置为边界子视图的框架,而不是传递单元的contentViewUIImageView属性!

的修复方法如下:

代码语言:javascript
复制
@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
票数 50
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13735382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档