iOS UICollectionView原型单元格大小属性被忽略 [英] iOS UICollectionView prototype cell size property ignored

查看:289
本文介绍了iOS UICollectionView原型单元格大小属性被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有两个原型单元的UICollectionView。原型单元格具有不同的宽度并包含不同的控件(图像视图和Web视图)。我肯定会返回给定索引的正确原型单元格(所有单元格显示正确的内容),但忽略原型单元格大小,而是使用集合视图的项目大小。这不像是我手动设置大小。如果在实际显示属性时忽略该属性,那么允许在故事板中调整原型单元的大小是什么意思?

I'm using a UICollectionView with two prototype cells. The prototype cells have different widths and contain different controls (image view and web view). I'm definitely returning the correct prototype cell for a given index (all the cells display the correct content), but the prototype cell size is ignored and the collection view's item size is used instead. It's not like I'm manually setting the size. What's the point of allowing the prototype cell to be sized in storyboard if the property is just ignored when it's actually displayed?

推荐答案

故事板编辑器中单元格的大小只是为了帮助您设计单元格。由于每个原型单元可以是不同的大小,因此UICollectionView不知道要为所有单元格选择哪个单元的大小。这就是为什么要分别设置要用于单元格的实际大小的原因。您可以在设计器中通过选择集合视图并在大小检查器的集合视图大小标题下设置其单元格大小宽度和高度来完成此操作。

The size of the cell in the storyboard editor is just to help you design the cell. Since each prototype cell can be a different size, UICollectionView doesn't know which cell's size to pick for all the cells. That's why you set the actual size you want to use for your cells separately. You can do it in the designer by selecting the collection view and setting its Cell Size Width and Height in the Size inspector, under the "Collection View Size" heading.

或者,您可以覆盖以下方法并返回一个CGSize对象,该对象指定您要为每个单元格使用的大小。使用此方法,您实际上可以让每个单元格具有不同的大小:

Or, you can override the following method and return a CGSize object that specifies the size you want to use for each cell. Using this method, you can actually have each cell be a different size:

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

示例:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(100, 100);
}

您的视图控制器需要是UICollectionViewDelegateFlowLayout的委托才能使此方法叫做。所以不要忘记将该委托声明添加到视图控制器的.h文件中,例如:

Your view controller needs to be a delegate of UICollectionViewDelegateFlowLayout in order for this method to be called. So don't forget to add that delegate declaration to your view controller's .h file, such as:

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

这篇关于iOS UICollectionView原型单元格大小属性被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆