在 UICollectionViewCell 中隐藏 UIImage
Hiding UIImage in UICollectionViewCell
我目前的 UICollectionViewCell
看起来像这样:
有时候不想显示图片。在这种情况下,我希望调整 UICollectionViewCell
的大小以仅适合 tmp、tmp1 和 tmp2。
我将如何完成这项工作?
默认的 Collection View Delegate 可以帮助您管理 CollectionViewCell 的大小。
您可以放置条件并相应地调整大小。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (image)
{
CGSize size = CGSizeMake(CGRectGetWidth(self.view.frame), 500 ) ;
return size;
}
else
{
CGSize size = CGSizeMake(CGRectGetWidth(self.view.frame), 200 ) ;
return size;
}
}
如果您使用的是估计大小,那么您可以通过这个例子...SelfSizingCellDemo
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;
}}
在控制器中设置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);
}
我目前的 UICollectionViewCell
看起来像这样:
有时候不想显示图片。在这种情况下,我希望调整 UICollectionViewCell
的大小以仅适合 tmp、tmp1 和 tmp2。
我将如何完成这项工作?
默认的 Collection View Delegate 可以帮助您管理 CollectionViewCell 的大小。
您可以放置条件并相应地调整大小。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (image)
{
CGSize size = CGSizeMake(CGRectGetWidth(self.view.frame), 500 ) ;
return size;
}
else
{
CGSize size = CGSizeMake(CGRectGetWidth(self.view.frame), 200 ) ;
return size;
}
}
如果您使用的是估计大小,那么您可以通过这个例子...SelfSizingCellDemo
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;
}}
在控制器中设置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);
}