繁体   English   中英

如何在UICollectionView的首次出现时更改UICollectionViewCell元素的大小

[英]How to change size of the UICollectionViewCell elements on the first appearance of a UICollectionView

我需要用9个单元格构建一个UICollectionView ,并且第一个UICollectionViewCell元素应大于其他元素。 我这样做是这样的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (hero == YES) {
    CGRect cellbounds = cell.firstView.bounds;
    cellbounds.size = CGSizeMake(10, 10);
    cell.firstView.bounds = cellbounds;
}
else{

    CGRect cellbounds = cell.firstView.bounds;
    cellbounds.size = CGSizeMake(100, 100);
    cell.firstView.bounds = cellbounds;
}

问题在于,我第一次启动应用程序时,所有单元格都具有默认大小,并且只有在刷新CollectionViewCell之后才应用所需的大小(我已经使用layoutIfNeeded )。

- (void)viewWillLayoutSubviews
{
    [self.CollectionView layoutIfNeeded];
}

请告诉我,我做错了什么?

尝试调用[self.CollectionView reloadData]; 在控制器的viewDidAppear中。

您应该实现此委托方法,我假设您正在使用flowLayout。

- (CGSize)collectionView:(UICollectionView *)collectionView 
                  layout:(UICollectionViewLayout*)collectionViewLayout 
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

参考: UICollectionViewDelegateFlowLayout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM