我当前的 UICollectionViewCell
看起来像这样:
有时候不想显示图片。在这种情况下,我希望调整 UICollectionViewCell
的大小以仅适合 tmp、tmp1 和 tmp2。
我将如何完成这项工作?
最佳答案
Set the Constraint on height of Image View in the Storyboard or in the XIB. Drog the Constraint on your self UICollectionViewCell . If have an image, the Constraint.constant = your need. If have no image , the Constraint.constant = 0;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureConstraint;
@end
@implementation CollectionViewCell
- (void)setModel:(cellObject *)model{
_model = model;
self.firstLabelView.text = model.firstLabelText;
self.secondLabelView.text = model.secondLabelText;
if (model.haveImage) {
self.pictureView.image = [UIImage imageNamed:@"kitten"];
self.pictureConstraint.constant = 149;
} else {
self.pictureConstraint.constant = 0;
}}
在 Controller 中设置 itemSize。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
cellObject *model = self.hahaArray[indexPath.item];
CGFloat height;
if (model.haveImage) {
height = "your pictureConstraint.constant + thirdLabel.maxY + your bottom margin ;
} else{
height = thirdLabel.maxY + your bottom margin ;
}
return CGSizeMake(self.flowLayout.itemSize.width, height);
}
关于ios - 在 UICollectionViewCell 中隐藏 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345555/