我正在使用集合视图,其中单元格包含两个图像。现在我希望单元格高度在图像高度的基础上是动态的。这是代码
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];
if (indexPath.section==[dataArray count]) {
return CGSizeMake(screenWidth,50);;
}else{
PMNewsFeedObject *feedObj=[dataArray objectAtIndex:indexPath.section];
if (feedObj.captionPopulateMore) {
float height=[self findHeightForText:feedObj.caption havingWidth:screenWidth-30 andFont:[UIFont fontWithName:@"MSReferenceSansSerif" size:15]];
height=height-10;
return CGSizeMake(feedObj.imageSize.width, feedObj.imageSize.height+height);
}else{
return CGSizeMake(feedObj.imageSize.width, feedObj.imageSize.height+45);
}
}
return CGSizeMake(0,0);
这是cellforitematindexpath的代码
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==dataArray.count) {
UICollectionViewCell *loadmoreCell=[homeCollectionView dequeueReusableCellWithReuseIdentifier:@"loadmoreCell" forIndexPath:indexPath];
UIActivityIndicatorView *loadmoreIndicator=(UIActivityIndicatorView*)[loadmoreCell viewWithTag:100];
[loadmoreIndicator startAnimating];
return loadmoreCell;
}
PMNewsFeedObject *feedObj=[dataArray objectAtIndex:indexPath.section];
PMHomeCollectionViewCell *cell=[homeCollectionView dequeueReusableCellWithReuseIdentifier:@"homeCell" forIndexPath:indexPath];
NSString *imageURLString=feedObj.petImage;
NSString *image2URLString = feedObj.petImage2;
if (imageURLString && ![imageURLString isEqual:[NSNull null]])
{
NSURL *imageURL = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",BASE_URL,imageURLString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cell.petImgView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder_pet.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[UIView animateWithDuration:0.0f animations:^{
UIImage *newImg=[image resizedImageByWidth:flowLayout.itemSize.width];
PMNewsFeedObject *newObject=[dataArray objectAtIndex:indexPath.section];
NSLog(@"%f",flowLayout.itemSize.width);
NSLog(@"%f",flowLayout.itemSize.height);
// float hfactor = image.size.width/flowLayout.itemSize.width;
// float vfactor = image.size.height /flowLayout.itemSize.height;
//
// NSLog(@"%f",flowLayout.itemSize.width);
// NSLog(@"%f",flowLayout.itemSize.height);
//
// float newHeight=0;
// if (image.size.width<image.size.height) {
//
// float factor = fmax(hfactor, vfactor);
// newHeight= image.size.height/factor;
// }else{
//
//
// float factor = fminf(hfactor, vfactor);
// newHeight= image.size.height * factor;
//
//
// }
// NSLog(@"%f",newImg.size.height);
if (newImg.size.height!=0) {
newObject.imageSize = newImg.size;
NSLog(@"%f",newImg.size.width);
NSLog(@"%f",newImg.size.height);
}
[dataArray replaceObjectAtIndex:indexPath.section withObject:newObject];
[self.homeCollectionView.collectionViewLayout invalidateLayout];
}];
}];
但我得到的所有单元格高度相同,图像在其中伸展。 这是一个单元格的屏幕截图
答案 0 :(得分:0)
要修复拉伸图像,请设置
_imageView.contentMode = UIViewContentModeScaleAspectFill;
关于集合视图单元格的大小,将断点放入cellForItemAtIndexPath
并检查计算错误或正确。