我想在iPhone上使用UICollectionView在网格中显示图像,每行显示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
我的jpg子类的部分代码,其中'imageNames‘是一个保存所有UICollectionViewController文件名的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和文件名之间的映射是正确的。
有谁知道是什么导致了这种奇怪的行为吗?
发布于 2013-01-21 20:41:59
同时,我已经找到了解决方案。这实际上是我的UICollectionViewCell
子类AlbumCoverCell
实现中的一个非常细微的错误。
问题是,我已经将单元实例的框架设置为边界子视图的框架,而不是传递单元的contentView
的UIImageView
属性!
的修复方法如下:
@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
https://stackoverflow.com/questions/13735382
复制相似问题