xcode视图集合单元大小错误

时间:2013-06-04 19:16:15

标签: cell collectionview

我有一个自定义收集单元格笔尖,其大小为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;


}

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

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