6

When using my custom UICollectionViewLayout subclass, cellForItemAtIndexPath: is not called (I checked using breakpoints and debug output). This is how i use the custom layout:

- (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"];
    }

But when i change the above code to use UICollectionViewFlowLayout instead of my custom subclass, cellForItemAtIndexPath: is called.

Could it be that some code from my custom layout subclass prevents cellForItemAtIndexPath from being called?

flather
  • 165
  • 1
  • 8
  • Yes it could. You'll have to show some of it - if the layout doesn't return any size for the collection view, for example, or any size for the layout objects, then a cell will never be created. Have you implemented all the methods as described in the documentation? – jrturton Oct 07 '13 at 15:57
  • Thanks a lot. I messed up the cells frame origin... it was outside the visible area. So it was correct that `cellForItemAtIndexPath` was not called. – flather Oct 07 '13 at 16:15
  • Ok, I've put my comment as an answer then so you can close this one off. – jrturton Oct 07 '13 at 18:34

1 Answers1

5

If the layout doesn't return any size for the collection view, for example, or any size for the layout objects, then a cell will never be created. Also, the frames in the layout objects have to be within the visible area of the collection view.

jrturton
  • 118,105
  • 32
  • 252
  • 268