ios - 在 UICollectionViewCell 中隐藏 UIImage

标签 ios objective-c uiimage uicollectionviewcell

我当前的 UICollectionViewCell 看起来像这样:

just a picture

有时候不想显示图片。在这种情况下,我希望调整 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/

相关文章:

ios - 延迟加载带有事件指示器的 CollectionView

ios - 无法从一个 TextInput 点按到另一个

ios - 捆绑标识符和产品名称中区分大小写

ios - 为什么我无法在我的 AppDelegate 中以编程方式更改我的初始 View Controller ?

objective-c - 具有透明背景的 UITableView 具有纯黑色边框

ios - AVFoundation 照片大小和旋转

iphone - 从 UIView 创建 UIImage 时出现非空无效上下文错误

iOS 9 Safari : changing an element to fixed position while scrolling won't paint until scroll stops

ios - Facebook iOS SDK DemoApp 即使在配置 App ID 后也显示 "Sorry, an error has occurred"

objective-c - 如何在 Objective-C 中添加观察者并在 Swift 3 中触发通知