1

I'm trying to use RFQuiltLayout with my UICollectionView.

I have the CollectionView working fine with the standard UICollectionViewFlowLayout but it's just a grid. I want my photos to layout nicely as shown here.

I'm having some trouble understanding what's required to use a custom layout with my UICollectionView.

I'm doing everything programatically. I'm not using any Interface Builder/NIBs/Storyboards.

Getting the UICollectionView to layout with the standard FlowLayout was easy, but when I tried to change to the RFQuiltLayout I ran into the following error:

'UICollectionView must be initialized with a non-nil layout parameter'

This seems to be a common error, but none of the suggestions/answers on the other questions helped me resolve it.

So here's the relevant portions of my code:

.h

@interface PFRootViewController : UIViewController <OFFlickrAPIRequestDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;

.m

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

//Used to use the above standard FlowLayout, but now trying to move to the below RFQuiltLayout

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 40);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];



self.collectionView.delegate = self;
self.collectionView.dataSource = self;

[self.collectionView registerClass:[PFUICollectionViewCell class] forCellWithReuseIdentifier:@"FlickrCell"];


//Only Layout delegate method i've implemented:
- (CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0)
        return CGSizeMake(80, 40);

    return CGSizeMake(40, 80);
}

I really don't know what else I've got to do to get this working. If I simply remove the RFQuiltLayout references and uncomment my UICollectionViewFlowLayout alloc init everything works, albeit with the standard FlowLayout.

So if anyone can help point me in the right direction that would be great, this should be simple but it's been a real pain to try and get working. - UICollectionViews aren't as simple as UITableViews.

I spent a good portion of the day yesterday trying to get various custom layouts for my UICollectionView working (including RFQuiltLayout) without any success.

Any help is greatly appreciated,

Regards, John

Woodstock
  • 22,184
  • 15
  • 80
  • 118

1 Answers1

6

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];

Here you're asking the collection view for a layout object, two lines before you initialise the collection view. Compare with the line you commented out:

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

You need to create a new layout object:

RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];

Then assign it to your view as present.

Looking at the README of the repository, the problem is that you are copying code from a viewDidLoad method, which assumes that the collection view and its layout have been initialised from a storyboard. If you're creating your view in code, you need to create the layout as well.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Well it's easy when you put it like that... :) Let me try this solution, and I'll mark as correct if that works, thanks. – Woodstock Feb 12 '14 at 09:49
  • Thank you! Looks like i'm half way there, I just alloc and inited as suggested and I don't have the nil layout error anymore, however my screen is still black when I load the app, If I comment out the RFQuilt lines and just use the standard flow layout I see tiles, any ideas? - PS will award bounty as soon as I'm allowed by SO. – Woodstock Feb 12 '14 at 19:42
  • 1
    infact, when I dynamically set the layout of the collectionView after launching with the standard flow layout successfully, the collectionView just goes blank, and cellForItemAtIndexPath isn't called, not sure why! – Woodstock Feb 12 '14 at 21:56
  • 2
    Looks like you also need to set yourself as the delegate of the layout. – jrturton Feb 12 '14 at 21:59
  • I just figured that out, man how stupid of me! thanks for the help, bounty will award soon. I really dislike IB and storyboards, it makes the sample code so difficult to follow! – Woodstock Feb 12 '14 at 22:04