【问题标题】:xcode view collection cell wrong sizexcode 视图集合单元格大小错误
【发布时间】:2013-06-04 19:16:15
【问题描述】:

我有一个尺寸为 w:160 h:146 的自定义收集单元笔尖。当我填充我的集合视图时,所有元素都非常小,比如 20px * 20px。

为什么它会减小我的单元格的大小?这是我的代码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)IndexPath
{
    static NSString *CellIdentifier = @"Cell1";

    [collectionView registerNib:[UINib nibWithNibName:@"TjansterCell" bundle:nil] forCellWithReuseIdentifier:@"Cell1"];


    TjansterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:IndexPath];

    return cell;


}

【问题讨论】:

    标签: cell collectionview


    【解决方案1】:

    问题在于集合视图流布局使用其“itemSize”属性的默认大小。幸运的是,这很容易解决。只需将以下代码添加到您的集合视图的委托中:

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        return CGSizeMake(<your width>, <your height>);
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,当我运行应用程序时,我的自定义尺寸仍然是标准尺寸。因此,为了解决这个问题,我发现通过先手动调整情节提要中的单元格大小,然后在尺寸检查器中输入或重新输入自定义尺寸解决了这个错误的问题。 另外,我发现当您在情节提要中选择集合视图并选择尺寸检查器时。在这里,您在 Collection View Size 下输入正确的尺寸,然后在 Cell size 文本字段中输入尺寸。这是重要的部分。

      【讨论】: