2014-04-27 54 views
0

我想用自定义UICollectionViewCells实现侧滚动UICollectionView。每个自定义单元格都包含一个应填充单元格的UIImageView。现在我设置像这样的UIImageView的大小,如何使用填充单元格的UIImageView初始化自定义UICollectionViewCell?

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

static NSString *cellID = @"cellID"; 
THCVCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 
cell.imageView.image = [self.images objectAtIndex:(indexPath.row % [self.images count])]; 
cell.imageView.frame = cell.bounds; 

return cell; 
} 

但直到电池出列的重用的UIImageView不填充单元格。

我应该改变什么让UIImageView像第一次显示时那样出现?

回答

0

什么如果THCVCell.m里面,你加入这样的事情:

- (void) layoutSubviews { 
    _imageView.frame = self.contentView.bounds; 
} 

这样一来,如果小区发生了改变,大小,ImageView的将进行调整。

然后取出:

cell.imageView.frame = cell.bounds; 
+0

谢谢洛根!这是一个快速解决方案。我的应用现在完美运行。 – trvslhlt

+0

很高兴听到它的工作,与您的其他项目祝你好运! – Logan

0

请尝试以下操作,这会使imageView在单元格大小调整时调整大小。

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

static NSString *cellID = @"cellID"; 
THCVCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 
cell.imageView.image = [self.images objectAtIndex:(indexPath.row % [self.images count])]; 
cell.imageView.frame = cell.contentView.bounds; 
cell.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

return cell; 
} 
相关问题