繁体   English   中英

如何使用填充单元格的UIImageView初始化自定义UICollectionViewCell?

[英]How do I initialize a custom UICollectionViewCell with a UIImageView that fills the cell?

我正在尝试使用自定义UICollectionViewCells实现侧面滚动UICollectionView。 每个自定义单元格都包含一个应填充该单元格的UIImageView。 现在,我正在像这样设置UIImageView的大小,

-(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像首次显示单元格时那样显示?

请尝试以下操作,这将使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;  
}

如果在THCVCell.m内部添加了以下内容怎么办:

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

这样,如果单元格曾经改变过大小,则imageView将适应。

然后删除:

cell.imageView.frame = cell.bounds;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM