在第一个实例上,内容未在 UICollectionViewCell 内调整大小
Posted
技术标签:
【中文标题】在第一个实例上,内容未在 UICollectionViewCell 内调整大小【英文标题】:Content not resizing inside UICollectionViewCell on first instance 【发布时间】:2016-01-24 08:29:51 【问题描述】:我的 UICollectionView 有一个带有自动布局约束的 UIImageView。我需要连续固定数量的单元格,因此如果单元格需要,需要缩放图像。
即使我没有调整单元格的大小,而是在情节提要中设置 UIimageView 大于单元格的预期尺寸,第一次填充单元格时,图像大小是不正确的,但是当每个单元格随后被重复使用时,它们会完美地缩放。
一旦细胞被重复使用,缩放就会起作用,但请问我错过了什么?
example view 是第二次调用 reloaddata 在视图中下方的“球”。第一次调用填充了前 4 个单元格。第二个调用更正了现在重用的单元格,但未能缩放 4 个新单元格的内容。
下面是一些要求的代码。 generalBreakCollection 是焦点:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
if (collectionView == self.p1HiBreakCollection)
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"P1Cell" forIndexPath:indexPath];
UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:106];
ballShot *selectedBall = [self.p1BreakBalls objectAtIndex:indexPath.row];
cell.ball = [self.p1BreakBalls objectAtIndex:indexPath.row];
collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];
return cell;
else if (collectionView == self.p2HiBreakCollection)
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"P2Cell" forIndexPath:indexPath];
UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:108];
ballShot *selectedBall = [self.p2BreakBalls objectAtIndex:indexPath.row];
cell.ball = [self.p2BreakBalls objectAtIndex:indexPath.row];
collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];
return cell;
else
// generalBreakCollection
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"GeneralCell" forIndexPath:indexPath];
ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
cell.ball = [self.breakShots objectAtIndex:indexPath.row];
cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:@"highlighted_%@",selectedBall.imageNameLarge]];
cell.ballStoreImage.contentMode = UIViewContentModeScaleAspectFit;
return cell;
调整大小块包含:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
if (collectionView == self.generalBreakCollection)
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 10.0; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGSize size = CGSizeMake(cellWidth, cellWidth);
return size;
else
CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];
return defaultSize ;
我有自己的 UICollectionViewCell 类和 UIImageView 属性 ballStoreImage。我没有用它做太多,但我已将它链接到情节提要中单元格内的 UIImageView:
@property (strong, nonatomic) IBOutlet UIImageView *ballStoreImage;
【问题讨论】:
你能给我们看一些代码吗? 在此链接上找到解决方案:link 在我添加代码行之前,无法识别自动布局:[[cell contentView] setFrame:[cell bounds]]; 【参考方案1】:在我添加代码行之前无法识别自动布局:
[[cell contentView] setFrame:[cell bounds]];
所以完整的块读取:
else
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"breakCell" forIndexPath:indexPath];
[[cell contentView] setFrame:[cell bounds]];
ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
cell.ball = [self.breakShots objectAtIndex:indexPath.row];
cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:@"highlighted_%@",selectedBall.imageNameLarge]];
return cell;
【讨论】:
以上是关于在第一个实例上,内容未在 UICollectionViewCell 内调整大小的主要内容,如果未能解决你的问题,请参考以下文章