cellForItemAtIndexPath:使用自定义UICollectionViewLayout子类时不调用

问题描述:

使用我的自定义UICollectionViewLayout子类时,不调用cellForItemAtIndexPath:(我使用断点和调试输出进行检查)。这是我如何使用自定义布局:cellForItemAtIndexPath:使用自定义UICollectionViewLayout子类时不调用

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     DMSGridLayout* gridLayout = [[DMSGridLayout alloc] init]; 

//  UICollectionViewFlowLayout* flow = [[UICollectionViewFlowLayout alloc] init]; 
//  [flow setItemSize:CGSizeMake(150, 150)]; 

     UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:gridLayout]; 
     collection.delegate = self; 
     collection.dataSource = self; 
     self.collectionView = collection; 

     [self.view addSubview:self.collectionView]; 
     [self.collectionView registerClass:[DMSGridCell class] forCellWithReuseIdentifier:@"CollectionCell"]; 
    } 

但是,当我改变上面的代码使用UICollectionViewFlowLayout而不是我的自定义子类,cellForItemAtIndexPath:被调用。

难道是我的自定义布局子类中的一些代码阻止cellForItemAtIndexPath被调用吗?

+0

是的,它可以。您必须展示其中的一部分 - 例如,如果布局未为集合视图返回任何大小,或者没有为布局对象返回任何大小,则不会创建单元格。您是否按照文档中所述实施了所有方法? – jrturton

+0

非常感谢。我搞乱了细胞框架的起源......它在可见区域之外。所以没有调用“cellForItemAtIndexPath”是正确的。 – flather

+0

好的,我已经把我的评论作为答案,然后你可以关闭这一个。 – jrturton

如果布局未返回任何大小的集合视图,例如,或任何大小的布局对象,然后单元将永远不会创建。此外,布局对象中的框架必须位于集合视图的可见区域内。