我的UICollectionView有一个带有autolayout约束的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;
答案 0 :(得分:2)
在添加代码行之前,自动布局无法识别:
[[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;
}